summaryrefslogtreecommitdiffstats
path: root/lib/Text/Handlebars.pm
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2012-09-28 17:32:46 -0500
committerJesse Luehrs <doy@tozt.net>2012-09-28 17:32:46 -0500
commitc964148db9790676e892265327f567939619c349 (patch)
tree8823cc12b17e18d1c17cd2f472a6933f0c89e607 /lib/Text/Handlebars.pm
parent5d1c22c9436077c5e3dede4abe55dbc8713a1e4c (diff)
downloadtext-handlebars-c964148db9790676e892265327f567939619c349.tar.gz
text-handlebars-c964148db9790676e892265327f567939619c349.zip
get blocks working
Diffstat (limited to 'lib/Text/Handlebars.pm')
-rw-r--r--lib/Text/Handlebars.pm51
1 files changed, 51 insertions, 0 deletions
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;