Permalink
Browse files

multiple IF statements. widget testcase

  • Loading branch information...
gshank committed Sep 28, 2009
1 parent 2af88ff commit 1aa0c696c0cbf3bcef4ea55e83aa6799edd0629d
Showing with 43 additions and 14 deletions.
  1. +14 −10 lib/Template/Tiny.pm
  2. +25 −2 t/template.t
  3. +4 −2 t/tmpl_widgets.t
View
@@ -125,7 +125,8 @@ sub compile_tmpl {
my ( $self, $AST ) = @_;
my $current_level = 0;
- my $current_stash = 0;
+ my @current_stash;
+ push @current_stash, $current_level;
my $code = '';
if ( !$current_level ) {
$code .= $TMPL_CODE_START;
@@ -139,23 +140,26 @@ sub compile_tmpl {
}
elsif ( $type eq 'VARS' ) {
$code .=
- q{ $out .= $stash_} . $names[$current_stash] . q{->get(} .
+ q{ $out .= $stash_} . $names[$current_stash[-1]] . q{->get(} .
quote_lists(@$val) . qq{);\n};
}
elsif ( $type eq 'END' ) {
$code .= " }\n";
$current_level--;
- $current_stash--;
+ pop @current_stash;
}
elsif ( $type eq 'SECTION' ) {
- my $old = $names[$current_stash];
- my $new = $names[ ++$current_stash ];
+ my $cur = $current_stash[-1];
+ my $old = $names[$cur];
+ push @current_stash, $cur + 1;
$current_level++;
+ my $new = $names[ $current_stash[-1] ];
$code .= " for my \$stash_$new ( \$stash_$old\->sections('$val') ) {\n";
}
elsif ( $type eq 'IF' ) {
+ push @current_stash, $current_stash[-1];
$current_level++;
- my $cur = $names[$current_stash];
+ my $cur = $names[$current_stash[-1]];
$code .= " if ( \$stash_$cur->get('$val') ) {\n";
}
elsif ( $type eq 'CONCAT' ) {
@@ -166,7 +170,7 @@ sub compile_tmpl {
}
elsif ( $t eq 'VARS' ) {
$code .=
- q{ $out .= $stash_} . $names[$current_stash] . q{->get(} .
+ q{ $out .= $stash_} . $names[$current_stash[-1]] . q{->get(} .
quote_lists(@$val) . qq{)};
}
for my $concat (@$val) {
@@ -178,7 +182,7 @@ sub compile_tmpl {
}
elsif ( $ct eq 'VARS' ) {
$code .=
- qq{\n . \$stash_} . $names[$current_stash] . q{->get(qw(} .
+ qq{\n . \$stash_} . $names[$current_stash[-1]] . q{->get(qw(} .
join( ' ', @$cv ) . qq{))};
}
}
@@ -194,7 +198,7 @@ sub compile_tmpl {
return $code;
}
-sub _add_tmpl {
+sub add_template {
my ( $self, $tmpl_name, $tmpl_str ) = @_;
my $AST = $self->parse_tmpl($tmpl_str);
$AST = $self->_optimize_tmpl($AST);
@@ -208,7 +212,7 @@ sub process_str {
my $compiled_tmpl;
unless ( $compiled_tmpl = $self->_get_template($tmpl_name) ) {
- $compiled_tmpl = $self->_add_tmpl($tmpl_name, $tmpl_str );
+ $compiled_tmpl = $self->add_template($tmpl_name, $tmpl_str );
}
return $self->process( $tmpl_name, $stash );
}
View
@@ -319,12 +319,35 @@ TEST: [% some_var %] [% IF reason %]reason="[% reason %]"[% END %]
stop test
END
-$DB::single=1;
-$tt->_add_tmpl('test_if', $template);
+$tt->add_template('test_if', $template);
ok( $tt->_has_template('test_if'), 'template has been added' );
$out = $tt->process( 'test_if', { some_var => "Here it is" } );
ok( $out, 'got output' );
+my $widget = <<'END';
+<input type="text" name="[% html_name %]" id="[% id %]"
+ [% IF size %] size="[% size %]"[% END %]
+ [% IF maxlength %] maxlength="[% maxlength %]"[% END %]
+ value="[% fif %]">
+END
+
+$DB::single=1;
+$tt->add_template('widget', $widget);
+ok( $tt->_has_template('widget'), 'widget template added' );
+$out = $tt->process('widget', {
+ html_name => 'test_field',
+ id => 'abc1',
+ size => 40,
+ maxlength => 50,
+ fif => 'my_test',
+ });
+ok( $out, 'got output' );
+my $output =
+'<input type="text" name="test_field" id="abc1"
+ size="40"
+ maxlength="50"
+ value="my_test">';
+is( $out, $output, 'output matches' );
View
@@ -14,11 +14,12 @@ use Test::More;
[% IF maxlength %] maxlength="[% maxlength %]"[% END %]
value="[% fif %]">
END
- has 'template' => ( is => 'ro', isa => 'Template::Tiny', builder => 'build_temlate');
+ has 'template' => ( is => 'ro', isa => 'Template::Tiny', builder => 'build_template');
sub build_template {
my $self = shift;
my $tt = Template::Tiny->new;
- $tt->_add_template('text_widget', $widget );
+ $tt->add_template('text_widget', $widget );
+ return $tt;
}
@@ -40,6 +41,7 @@ END
}
+use HTML::FormHandler::Field::Text;
my $field = HTML::FormHandler::Field::Text->new_with_traits(
traits => ['Widget::Field::Text'], name => 'test_text' );
ok( $field, 'created field' );

0 comments on commit 1aa0c69

Please sign in to comment.