From: Uri Guttman Date: Sun, 14 Dec 2008 07:08:06 +0000 (-0500) Subject: added compiled loop support X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7dd120001373ca771b4baf231269e2296a908ce1;p=urisagit%2FTemplate-Simple.git added compiled loop support added compile scalar vs hash data support needs testing and benchmarking --- diff --git a/lib/Template/Simple.pm b/lib/Template/Simple.pm index 1b603d0..d4804ba 100644 --- a/lib/Template/Simple.pm +++ b/lib/Template/Simple.pm @@ -115,8 +115,7 @@ no warnings ; sub { my( \$data ) = \@_ ; - my \$out = - $code_body ; + my \$out = $code_body ; return \\\$out ; } @@ -139,24 +138,22 @@ sub _compile_chunk { return '' unless length $template ; - $indent .= "\t" ; - my @parts ; # loop all nested chunks and the text separating them while( $template =~ m{$self->{chunk_re}}g ) { -# grab the pre-chunk text and compile it for scalars and save all of its parts +# grab the pre-match text and compile its scalars and save all of its parts push @parts, $self->_compile_scalars( substr( $template, 0, $-[0] ) ) ; # compile the nested chunk and save its parts - push @parts, $self->_compile_chunk( $1, $2, $indent ) ; + push @parts, $self->_compile_chunk( $1, $2, "$indent\t\t" ) ; -# chop off the pre-chunk and chunk +# chop off the pre-match and the chunk substr( $template, 0, $+[0], '' ) ; } @@ -169,24 +166,46 @@ sub _compile_chunk { # start it with a do{} block open - my $code = "do {\n$indent" ; + my $code = <{$chunk_name} ; + $code .= <{$chunk_name} ; +CODE + +# add the loop code to handle a scalar or an array + + $code .= <{compiled_cache}{$template_name} ) { + unless( $tmpl_ref ) { - return $compiled->($data) ; - } +# render with cached code and return if we precompiled this template + + if ( my $compiled = $self->{compiled_cache}{$template_name} ) { -# TODO: look for template by name + return $compiled->($data) ; + } - my $template = eval{ $self->_get_template($1) } ; +# not compiled so get this template by name -print "GOT [$template]\n" ; + $tmpl_ref ||= eval{ $self->_get_template($template_name) } ; -# force the template into a ref +# we couldn't find this template name so assume it is the template text - my $tmpl_ref = ref $template eq 'SCALAR' ? $template : \$template ; + $tmpl_ref ||= \$template_name ; + } my $rendered = $self->_render_includes( $tmpl_ref ) ;