Hi,
In Dancer2, instead of using our own crappy default simple Template engine, we decided to use
your Template::Tiny. So far so good.
However, we are missing 2 features :
- be able to change the start / end tag (for isntance use <% instead of [%
- if the expression is a CodeRef, it should be evaluated.
Now, I have a patch for these two features. I have checked out the svn repo listed on metacpan,
and built the patches against it. Is it good enough or is there any other repo I should use ? Also,
I couldn't se any tests in the source. Is there an other tarball / source repo that has some tests
for T::Tiny ?
Anyway attached is the patch. It implements the 2 features above. Surely you might want to
refactor it, but you get the idea.
Are you open to have these features in T::Tiny? if yes, a quick release would mean we can rely
on it for the upcoming Dancer2 release. If not, please let me know, so I can work around that or
use an other simple Template engine in Dancer2.
Note that the implementation is not optimized, it may trigger too many dereferences and hash
lookup for you.
Thanks,
dams
Subject: | patch_template_tiny.diff |
Index: lib/Template/Tiny.pm
===================================================================
--- lib/Template/Tiny.pm (revision 15538)
+++ lib/Template/Tiny.pm (working copy)
@@ -10,26 +10,46 @@
# Evaluatable expression
my $EXPR = qr/ [a-z_][\w.]* /xs;
-# Opening [% tag including whitespace chomping rules
-my $LEFT = qr/
+# Condition set
+my $CONDITION = qr/
+ \[\%\s
+ ( ([IUF])\d+ ) \s+
+ (?:
+ ([a-z]\w*) \s+ IN \s+
+ )?
+ ( $EXPR )
+ \s\%\]
+ ( .*? )
(?:
- (?: (?:^|\n) [ \t]* )? \[\%\-
+ \[\%\s \1 \s\%\]
+ ( .+? )
+ )?
+ \[\%\s \1 \s\%\]
+/xs;
+
+sub new {
+ my $self = bless { start_tag => '[%',
+ end_tag => '%]', @_[1..$#_] }, $_[0];
+# Opening tag including whitespace chomping rules
+my $LEFT = $self->{LEFT} = qr/
+ (?:
+ (?: (?:^|\n) [ \t]* )? \Q$self->{start_tag}\E\-
|
- \[\% \+?
+ \Q$self->{start_tag}\E \+?
) \s*
/xs;
# Closing %] tag including whitespace chomping rules
-my $RIGHT = qr/
+my $RIGHT = $self->{RIGHT} = qr/
\s* (?:
- \+? \%\]
+ \+? \Q$self->{end_tag}\E
|
- \-\%\] (?: [ \t]* \n )?
+ \-\Q$self->{end_tag}\E (?: [ \t]* \n )?
)
/xs;
# Preparsing run for nesting tags
-my $PREPARSE = qr/
+$self->{PREPARSE} = qr/
$LEFT ( IF | UNLESS | FOREACH ) \s+
(
(?: \S+ \s+ IN \s+ )?
@@ -50,26 +70,7 @@
)?
$LEFT END $RIGHT
/xs;
-
-# Condition set
-my $CONDITION = qr/
- \[\%\s
- ( ([IUF])\d+ ) \s+
- (?:
- ([a-z]\w*) \s+ IN \s+
- )?
- ( $EXPR )
- \s\%\]
- ( .*? )
- (?:
- \[\%\s \1 \s\%\]
- ( .+? )
- )?
- \[\%\s \1 \s\%\]
-/xs;
-
-sub new {
- bless { @_[1..$#_] }, $_[0];
+$self;
}
# Copy and modify
@@ -120,7 +121,7 @@
# Preprocess to establish unique matching tag sets
my $id = 0;
1 while $$copy =~ s/
- $PREPARSE
+ $self->{PREPARSE}
/
my $tag = substr($1, 0, 1) . ++$id;
"\[\% $tag $2 \%\]$3\[\% $tag \%\]"
@@ -148,7 +149,7 @@
# Resolve expressions
$text =~ s/
- $LEFT ( $EXPR ) $RIGHT
+ $self->{LEFT} ( $EXPR ) $self->{RIGHT}
/
eval {
$self->_expression($stash, $1)
@@ -199,6 +200,9 @@
return '';
}
}
+ # If the last expression is a CodeRef, execute it
+ ref($cursor) eq 'CODE'
+ and $cursor = $cursor->();
return $cursor;
}
@@ -242,8 +246,17 @@
=head2 SUPPORTED USAGE
-Only the default C<[% %]> tag style is supported.
+By default, the C<[% %]> tag style is used. You can change the start tag and
+end tag by specifying them at object creation :
+ my $template = Template::Tiny->new(
+ start_tag => '<%',
+ end_tag => '%>,
+ );
+
+In the rest of the documentation, C<[% %]> will be used, but it can be of
+course your specified start / end tags.
+
Both the C<[%+ +%]> style explicit whitespace and the C<[%- -%]> style
explicit chomp B<are> support, although the C<[%+ +%]> version is unneeded
in practice as B<Template::Tiny> does not support default-enabled C<PRE_CHOMP>
@@ -255,6 +268,8 @@
objects are supported. "VMethods" such as [% array.length %] are B<not>
supported at this time.
+If the resulting expression is a CodeRef, it'll be evaluated.
+
C<IF>, C<ELSE> and C<UNLESS> conditional blocks B<are> supported, but only with
simple C<[% foo.bar.baz %]> conditions.