From: Uri Guttman Date: Wed, 24 Dec 2008 06:36:30 +0000 (-0500) Subject: added compiled template support X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=urisagit%2FTemplate-Simple.git;a=commitdiff_plain;h=9d1a25cd957199779463a643b0267c50984005a0;hp=107d449430218653cdae78a6588ec35e66137492 added compiled template support --- diff --git a/lib/Template/Simple.pm b/lib/Template/Simple.pm index 4dd68a4..165f132 100644 --- a/lib/Template/Simple.pm +++ b/lib/Template/Simple.pm @@ -4,10 +4,10 @@ use warnings; use strict; use Carp ; +use Data::Dumper ; use Scalar::Util qw( reftype ) ; use File::Slurp ; -use Data::Dumper ; our $VERSION = '0.03'; @@ -109,14 +109,14 @@ sub compile { my $code_body = $self->_compile_chunk( '', "${$tmpl_ref}", "\t" ) ; my $source = <{source_cache}{$template_name} = $source ; } -sub get_source { +sub _compile_chunk { - my( $self, $template_name ) = @_ ; + my( $self, $chunk_name, $template, $indent ) = @_ ; - return $self->{source_cache}{$template_name} ; -} + return '' unless length $template ; +# generate a lookup in data for this chunk name (unless it is the top +# level). this descends down the data tree during rendering -sub _compile_chunk { + my $data_init = $chunk_name ? "\$data->{$chunk_name}" : '$data' ; - my( $self, $chunk_name, $template, $indent ) = @_ ; + my $code = <{chunk_re}} ) { -# grab the pre-match text and compile its scalars and save all of its parts +# get the pre-match text and compile its scalars and text. append to the code - push @parts, $self->_compile_scalars( - substr( $template, 0, $-[0] ) ) ; + $code .= $self->_compile_scalars( + substr( $template, 0, $-[0] ), $indent ) ; # print "OFF: $-[0] $+[0]\n" ; # print "PRE: [", substr( $template, 0, $-[0] ), "]\n\n" ; @@ -162,95 +170,43 @@ sub _compile_chunk { # print "TRUNC: [", substr( $template, 0, $+[0] ), "]\n\n" ; # print "LEFT: [$template]\n\n" ; -# compile the nested chunk and save its parts +# compile the nested chunk and append to the code - push @parts, $self->_compile_chunk( $1, $2, "$indent\t\t" ) ; + $code .= $self->_compile_chunk( $1, $2, $indent ) ; # chop off the pre-match and the chunk substr( $template, 0, $+[0], '' ) ; - -# print "LEFT2: [$template]\n\n" ; -# print Dumper \@parts ; - } -# compile trailing text for scalars and save all of its parts - - push @parts, $self->_compile_scalars( $template ) ; +# compile trailing text for scalars and append to the code -# generate the code for this chunk - -# start it with a do{} block open - - my $code = <{$chunk_name} ; -CODE - -# add the loop code to handle a scalar or an array - - $code .= <_compile_scalars( $template, $indent ) ; chop $indent ; -# now we end the .= statement, the loop and the do block for this chunk +# now we end the loop for this chunk $code .= <{scalar_re}}g ) { -# keep the text before the scalar markup and the code to access the scalar +# get the pre-match text before the scalar markup and generate code to +# access the scalar push( @parts, dump_text( substr( $template, 0, $-[0] ) ), @@ -266,10 +222,16 @@ sub _compile_scalars { push @parts, dump_text( $template ) ; - return @parts ; + my $parts_code = join( "\n$indent.\n$indent", @parts ) ; + + return <{source_cache}{$template_name} ; +} + sub render { my( $self, $template_name, $data ) = @_ ;