summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/Text/Handlebars.pm8
-rw-r--r--lib/Text/Xslate/Syntax/Handlebars.pm16
-rw-r--r--t/expressions.t16
3 files changed, 37 insertions, 3 deletions
diff --git a/lib/Text/Handlebars.pm b/lib/Text/Handlebars.pm
index aa239a0..c58ae86 100644
--- a/lib/Text/Handlebars.pm
+++ b/lib/Text/Handlebars.pm
@@ -4,6 +4,8 @@ use warnings;
use base 'Text::Xslate';
+use Scalar::Util 'weaken';
+
sub default_functions {
my $class = shift;
return {
@@ -35,7 +37,11 @@ sub default_functions {
die "invalid value: $value"
if !defined($ref) || $ref ne 'HASH';
- return $value;
+ weaken(my $vars_copy = $vars);
+ return {
+ %$value,
+ '..' => $vars_copy,
+ };
}
else {
return $vars;
diff --git a/lib/Text/Xslate/Syntax/Handlebars.pm b/lib/Text/Xslate/Syntax/Handlebars.pm
index 3537952..9bd1366 100644
--- a/lib/Text/Xslate/Syntax/Handlebars.pm
+++ b/lib/Text/Xslate/Syntax/Handlebars.pm
@@ -148,6 +148,7 @@ sub init_symbols {
$self->prefix('/', 0)->is_block_end(1);
$self->prefix('&', 0)->set_nud($self->can('nud_mark_raw'));
+ $self->prefix('..', 0)->set_nud($self->can('nud_uplevel'));
}
sub nud_name {
@@ -290,6 +291,13 @@ sub nud_mark_raw {
return $self->call('mark_raw', $self->expression(0));
}
+sub nud_uplevel {
+ my $self = shift;
+ my ($symbol) = @_;
+
+ return $symbol->clone(arity => 'variable');
+}
+
sub make_field_lookup {
my $self = shift;
my ($var, $field, $dot) = @_;
@@ -307,6 +315,14 @@ sub make_field_lookup {
);
}
+sub is_valid_field {
+ my $self = shift;
+ my ($field) = @_;
+
+ return 1 if $field->id eq '..';
+ return $self->SUPER::is_valid_field(@_);
+}
+
sub make_ternary {
my $self = shift;
my ($if, $then, $else) = @_;
diff --git a/t/expressions.t b/t/expressions.t
index 02b1b3a..c2e928c 100644
--- a/t/expressions.t
+++ b/t/expressions.t
@@ -26,7 +26,18 @@ render_ok(
"/ separator"
);
-{ local $TODO = "unimplemented";
+render_ok(
+ '<h1>{{page.article.title}}</h1> - {{date}}',
+ {
+ page => {
+ article => { title => 'Multilevel field access' },
+ },
+ date => '2012-10-01',
+ },
+ '<h1>Multilevel field access</h1> - 2012-10-01',
+ "multilevel field access with ."
+);
+
render_ok(
'{{#article}}<h1>{{title}}</h1> - {{../date}}{{/article}}',
{ article => { title => 'Backtracking' }, date => '2012-10-01' },
@@ -34,6 +45,7 @@ render_ok(
"backtracking with ../"
);
+{ local $TODO = "autochomping issues";
render_ok(
<<'TEMPLATE',
{{#page}}
@@ -51,6 +63,7 @@ TEMPLATE
RENDERED
"multilevel backtracking with ../"
);
+}
render_ok(
'{{#article}}<h1>{{title}}</h1> - {{../metadata.date}}{{/article}}',
@@ -61,6 +74,5 @@ render_ok(
'<h1>Backtracking</h1> - 2012-10-01',
"backtracking into other hash variables with ../ and ."
);
-}
done_testing;