diff options
-rw-r--r-- | lib/Text/Handlebars.pm | 18 | ||||
-rw-r--r-- | lib/Text/Handlebars/Compiler.pm | 2 | ||||
-rw-r--r-- | lib/Text/Xslate/Syntax/Handlebars.pm | 17 | ||||
-rw-r--r-- | t/block-helpers.t | 26 | ||||
-rw-r--r-- | t/helpers-examples.t | 4 | ||||
-rw-r--r-- | t/helpers.t | 6 |
6 files changed, 55 insertions, 18 deletions
diff --git a/lib/Text/Handlebars.pm b/lib/Text/Handlebars.pm index 25075b1..a040504 100644 --- a/lib/Text/Handlebars.pm +++ b/lib/Text/Handlebars.pm @@ -90,6 +90,7 @@ sub options { my $options = $class->SUPER::options(@_); $options->{compiler} = 'Text::Handlebars::Compiler'; + $options->{helpers} = {}; return $options; } @@ -128,6 +129,23 @@ sub _register_builtin_methods { return $code->($vars, @args, $options); }; + + for my $helper (keys %{ $self->{helpers} }) { + $funcs->{$helper} = $self->{helpers}{$helper}; + } +} + +sub _compiler { + my $self = shift; + + if (!ref($self->{compiler})) { + my $compiler = $self->SUPER::_compiler(@_); + $compiler->define_helper(keys %{ $self->{helpers} }); + return $compiler; + } + else { + return $self->SUPER::_compiler(@_); + } } sub render_string { diff --git a/lib/Text/Handlebars/Compiler.pm b/lib/Text/Handlebars/Compiler.pm index ae1738b..184b9b6 100644 --- a/lib/Text/Handlebars/Compiler.pm +++ b/lib/Text/Handlebars/Compiler.pm @@ -9,6 +9,8 @@ has '+syntax' => ( default => 'Handlebars', ); +sub define_helper { shift->parser->define_helper(@_) } + sub _generate_block { my $self = shift; my ($node) = @_; diff --git a/lib/Text/Xslate/Syntax/Handlebars.pm b/lib/Text/Xslate/Syntax/Handlebars.pm index 252b4c9..a24fa00 100644 --- a/lib/Text/Xslate/Syntax/Handlebars.pm +++ b/lib/Text/Xslate/Syntax/Handlebars.pm @@ -5,6 +5,8 @@ use Any::Moose; use Carp 'confess'; use Text::Xslate::Util qw($DEBUG $NUMBER neat p); +use Text::Handlebars::Symbol; + extends 'Text::Xslate::Parser'; use constant _DUMP_PROTO => scalar($DEBUG =~ /\b dump=proto \b/xmsi); @@ -29,6 +31,8 @@ sub _build_tag_end { '}}' } sub _build_shortcut_table { +{} } +sub symbol_class { 'Text::Handlebars::Symbol' } + sub split_tags { my $self = shift; my ($input) = @_; @@ -584,6 +588,19 @@ sub define_function { return; } +sub define_helper { + my $self = shift; + my (@names) = @_; + + $self->define_function(@names); + for my $name (@names) { + my $symbol = $self->symbol($name); + $symbol->is_helper(1); + } + + return; +} + sub parse_literal { my $self = shift; my ($literal) = @_; diff --git a/t/block-helpers.t b/t/block-helpers.t index 5ca009a..4b68d1e 100644 --- a/t/block-helpers.t +++ b/t/block-helpers.t @@ -7,7 +7,7 @@ use Test::Handlebars; render_ok( { - function => { + helpers => { noop => sub { my ($context, $options) = @_; return $options->{fn}->($context); @@ -36,7 +36,7 @@ RENDERED render_ok( { - function => { + helpers => { with => sub { my ($context, $new_context, $options) = @_; return $options->{fn}->($new_context); @@ -71,7 +71,7 @@ RENDERED render_ok( { - function => { + helpers => { with => sub { my ($context, $new_context, $options) = @_; return $options->{fn}->($new_context); @@ -138,7 +138,7 @@ RENDERED render_ok( { - function => { + helpers => { list => sub { my ($context, $items, $options) = @_; my $out = "<ul>"; @@ -178,7 +178,7 @@ RENDERED render_ok( { - function => { + helpers => { if => sub { my ($context, $conditional, $options) = @_; if ($conditional) { @@ -204,7 +204,7 @@ RENDERED render_ok( { - function => { + helpers => { if => sub { my ($context, $conditional, $options) = @_; if ($conditional) { @@ -229,7 +229,7 @@ RENDERED render_ok( { - function => { + helpers => { if => sub { my ($context, $conditional, $options) = @_; if ($conditional) { @@ -259,7 +259,7 @@ RENDERED render_ok( { - function => { + helpers => { if => sub { my ($context, $conditional, $options) = @_; if ($conditional) { @@ -290,7 +290,7 @@ RENDERED { local $TODO = "unimplemented"; local $SIG{__WARN__} = sub { }; render_ok( { - function => { + helpers => { list => sub { my ($context, $items, $options) = @_; @@ -336,7 +336,7 @@ RENDERED # more reasonable. feedback welcome! render_ok( { - function => { + helpers => { list => sub { my ($context, $items, $options) = @_; @@ -380,7 +380,7 @@ RENDERED render_ok( { - function => { + helpers => { list => sub { my ($context, $items, $options) = @_; @@ -413,7 +413,7 @@ render_ok( render_ok( { - function => { + helpers => { list => sub { my ($context, $items, $options) = @_; @@ -443,7 +443,7 @@ render_ok( render_ok( { - function => { + helpers => { list => sub { my ($context, $items, $options) = @_; diff --git a/t/helpers-examples.t b/t/helpers-examples.t index 1df8e99..8f5a2fc 100644 --- a/t/helpers-examples.t +++ b/t/helpers-examples.t @@ -9,7 +9,7 @@ use Text::Xslate 'mark_raw'; render_ok( { - function => { + helpers => { fullName => sub { my ($context, $person) = @_; return $person->{firstName} . ' ' . $person->{lastName}; @@ -55,7 +55,7 @@ RENDERED render_ok( { - function => { + helpers => { agree_button => sub { my ($context) = @_; return mark_raw( diff --git a/t/helpers.t b/t/helpers.t index 98a72ba..51ab9ac 100644 --- a/t/helpers.t +++ b/t/helpers.t @@ -9,7 +9,7 @@ use Text::Xslate 'mark_raw'; render_ok( { - function => { + helpers => { link => sub { my ($context, $object) = @_; return mark_raw( @@ -37,7 +37,7 @@ RENDERED render_ok( { - function => { + helpers => { link => sub { my ($context, $text, $url) = @_; return mark_raw( @@ -63,7 +63,7 @@ RENDERED { local $TODO = "unimplemented"; render_ok( { - function => { + helpers => { link => sub { my ($context, $text, $options) = @_; |