From c964148db9790676e892265327f567939619c349 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 28 Sep 2012 17:32:46 -0500 Subject: get blocks working --- lib/Text/Handlebars.pm | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 lib/Text/Handlebars.pm (limited to 'lib/Text/Handlebars.pm') diff --git a/lib/Text/Handlebars.pm b/lib/Text/Handlebars.pm new file mode 100644 index 0000000..bad8c64 --- /dev/null +++ b/lib/Text/Handlebars.pm @@ -0,0 +1,51 @@ +package Text::Handlebars; +use strict; +use warnings; + +use base 'Text::Xslate'; + +sub default_functions { + my $class = shift; + return { + %{ $class->SUPER::default_functions(@_) }, + '(is_array)' => sub { + my ($val) = @_; + return ref($val) && ref($val) eq 'ARRAY'; + }, + '(make_array)' => sub { + my ($length) = @_; + return [(undef) x $length]; + }, + '(new_vars_for)' => sub { + my ($vars, $value, $i) = @_; + $i = 0 unless defined $i; # XXX + + if (my $ref = ref($value)) { + if (defined $ref && $ref eq 'ARRAY') { + die "no iterator cycle provided?" + unless defined $i; + $value = $value->[$i]; + $ref = ref($value); + } + + die "invalid value: $value" + if !defined($ref) || $ref ne 'HASH'; + + return $value; + } + else { + return $vars; + } + }, + }; +} + +sub options { + my $class = shift; + + my $options = $class->SUPER::options(@_); + $options->{compiler} = 'Text::Handlebars::Compiler'; + return $options; +} + +1; -- cgit v1.2.3-54-g00ecf