diff options
author | Jesse Luehrs <doy@tozt.net> | 2012-09-28 17:33:47 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2012-09-28 17:33:47 -0500 |
commit | 49db5f5704835b7336c67ecf4ba14a5a5f3a8527 (patch) | |
tree | ff79270672e31a46a0cd36d48b8c4e0f7a9a85d6 /t/helpers.t | |
parent | c964148db9790676e892265327f567939619c349 (diff) | |
download | text-handlebars-49db5f5704835b7336c67ecf4ba14a5a5f3a8527.tar.gz text-handlebars-49db5f5704835b7336c67ecf4ba14a5a5f3a8527.zip |
remove test numbers
Diffstat (limited to 't/helpers.t')
-rw-r--r-- | t/helpers.t | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/t/helpers.t b/t/helpers.t new file mode 100644 index 0000000..55b3b5a --- /dev/null +++ b/t/helpers.t @@ -0,0 +1,47 @@ +use strict; +use warnings; +use Test::More; +use Text::Xslate; + +my $tx = Text::Xslate->new(syntax => 'Handlebars'); + +# XXX I'm not sure how helpers should be registered in Perl +# in JS, it's global which is crappy +# Text::Xslate->new has a "function" parameter for registering helpers +Handlebars->registerHelper(noop => sub { + my ($context, $options) = @_; + return $options->{fn}->($context); +}); + +is( + $tx->render_string( + '<h1>{{title}}</h1><p>{{#noop}}{{body}}{{/noop}}</p>', + { title => 'A', body => 'the first letter' }, + ), + '<h1>A</h1><p>the first letter</p>', +); + +Handlebars->registerHelper(list => sub { + my ($items, $options) = @_; + my $out = "<ul>"; + + for my $item (@$items) { + $out .= "<li>" . $options->{fn}->($item) . "</li>"; + } + + return $out . "</ul>"; +}); + +is( + $tx->render_string( + '{{#list people}}{{firstName}} {{lastName}}{{/list}}', + { people => [ + { firstName => 'Jesse', lastName => 'Luehrs' }, + { firstName => 'Shawn', lastName => 'Moore' }, + { firstName => 'Stevan', lastName => 'Little' }, + ] }, + ), + '<ul><li>Jesse Luehrs</li><li>Shawn Moore</li><li>Stevan Little</li></ul>', +); + +done_testing; |