got compiler working
[urisagit/Template-Simple.git] / lib / Template / Simple.pm
index 4906108..4dd68a4 100644 (file)
@@ -108,28 +108,34 @@ sub compile {
 
        my $code_body = $self->_compile_chunk( '', "${$tmpl_ref}", "\t" ) ;
 
-       $self->{source} = <<CODE ;
+       my $source = <<CODE ;
 
 no warnings ;
 
 sub {
        my( \$data ) = \@_ ;
 
-       my \$out =
-       $code_body ;
+       my \$out = $code_body ;
 
        return \\\$out ;
 }
 CODE
 
-       $self->{source_cache}{$template_name} = $self->{source} ;
-       print $self->{source} ;
+#print $source ;
 
-       my $code_ref = eval $self->{source} ;
+       my $code_ref = eval $source ;
 
-die $@ if $@ ;
+print $@ if $@ ;
 
        $self->{compiled_cache}{$template_name} = $code_ref ;
+       $self->{source_cache}{$template_name} = $source ;
+}
+
+sub get_source {
+
+       my( $self, $template_name ) = @_ ;
+
+       return $self->{source_cache}{$template_name} ;
 }
 
 
@@ -139,26 +145,34 @@ 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 ) {
+       while( $template =~ m{$self->{chunk_re}} ) {
 
-# 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] ) ) ;
 
+# print "OFF: $-[0] $+[0]\n" ;
+# print "PRE: [", substr( $template, 0, $-[0] ), "]\n\n" ;
+# print "CHUNK: [$1] BODY [$2]\n\n" ;
+# print "TRUNC: [", substr( $template, 0, $+[0] ), "]\n\n" ;
+# print "LEFT: [$template]\n\n" ;
+
 # 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], '' ) ;
+
+# print "LEFT2: [$template]\n\n" ;
+# print Dumper \@parts ;
+
        }
 
 # compile trailing text for scalars and save all of its parts
@@ -169,24 +183,57 @@ sub _compile_chunk {
 
 # start it with a do{} block open
 
-       my $code = "do {\n$indent" ;
+       my $code = <<CODE ;
+do {
+CODE
+
+       $indent .= "\t" ;
 
 # generate a lookup in data for this chunk name (unless it is the top
 # level). this descends down the data tree during rendering
 
-       $code .= <<CODE . $indent if $chunk_name ;
-my \$data = \$data->{$chunk_name} ;
+       $code .= <<CODE if $chunk_name ;
+${indent}my \$data = \$data->{$chunk_name} ;
+CODE
+
+# add the loop code to handle a scalar or an array
+
+       $code .= <<CODE ;
+${indent}my \$out ;
+
+${indent}my \@data = \$data ;
+${indent}while( defined( my \$data = shift \@data ) ) {
+
+${indent}      if ( ref \$data eq 'ARRAY' ) {
+${indent}              push \@data, \@{\$data} ;
+${indent}              next ;
+${indent}      }
+
+       ${indent}\$out .= ref \$data ne 'HASH' ? \$data :
 CODE
 
+#${indent}foreach my \$data ( ref \$data eq 'ARRAY' ? \@{\$data} : \$data ) {
+
+
+
+       $indent .= "\t" ;
+
 # now generate the code to output all the parts of this chunk. they
 # are all concatentated by the . operator
 
-       $code .= join( "\n$indent.\n$indent", @parts ) ;
+       $code .= $indent . join( "\n$indent.\n$indent", @parts ) ;
+
+       chop $indent ;
 
-# now we close the do block
+# now we end the .= statement, the loop and the do block for this chunk
+       $code .= <<CODE ;
+ ;
+$indent}
+$indent\$out ;
+CODE
 
        chop $indent ;
-       $code .= "\n$indent}" ;
+       $code .= "$indent}" ;
 
        return $code ;
 }
@@ -209,6 +256,9 @@ sub _compile_scalars {
                        dump_text( substr( $template, 0, $-[0] ) ),
                        "\$data->{$1}"
                ) ;
+
+# truncate the matched text so the next match starts at begining of string
+
                substr( $template, 0, $+[0], '' ) ;
        }
 
@@ -241,22 +291,25 @@ sub render {
 
        my( $self, $template_name, $data ) = @_ ;
 
-# render with cached code if we precompiled this template
+       my $tmpl_ref = ref $template_name eq 'SCALAR' ? $template_name : '' ;
 
-       if ( my $compiled = $self->{compiled_cache}{$template_name} ) {
+       unless( $tmpl_ref ) {
 
-               return $compiled->($data) ;
-       }
+# render with cached code and return if we precompiled this template
 
-# TODO: look for template by name 
+               if ( my $compiled = $self->{compiled_cache}{$template_name} ) {
 
-       my $template = eval{ $self->_get_template($1) } ;
+                       return $compiled->($data) ;
+               }
 
-print "GOT [$template]\n" ;
+# not compiled so get this template by name
 
-# force the template into a ref
+               $tmpl_ref ||= eval{ $self->_get_template($template_name) } ;
 
-       my $tmpl_ref = ref $template eq 'SCALAR' ? $template : \$template ;
+# we couldn't find this template name so assume it is the template text
+
+               $tmpl_ref ||= \$template_name ;
+       }
 
        my $rendered = $self->_render_includes( $tmpl_ref ) ;
 
@@ -337,8 +390,8 @@ sub _render_hash {
        $rendered =~ s{$self->{chunk_re}}
                      {
                        # print "CHUNK $1\nBODY\n----\n<$2>\n\n------\n" ;
-                       print "CHUNK $1\nBODY\n----\n<$2>\n\n------\n" ;
-                       print "pre CHUNK [$`]\n" ;
+#                      print "CHUNK $1\nBODY\n----\n<$2>\n\n------\n" ;
+#                      print "pre CHUNK [$`]\n" ;
                        ${ $self->_render_chunk( \"$2", $href->{$1} ) }
                      }gex ;
 
@@ -409,9 +462,18 @@ sub delete_templates {
 
        my( $self, @names ) = @_ ;
 
+# delete all the cached stuff or just the names passed in
+
        @names = keys %{$self->{tmpl_cache}} unless @names ;
 
+# clear out all the caches
+# TODO: reorg these into a hash per name
+
        delete @{$self->{tmpl_cache}}{ @names } ;
+       delete @{$self->{compiled_cache}}{ @names } ;
+       delete @{$self->{source_cache}}{ @names } ;
+
+# also remove where we found it to force a fresh search
 
        delete @{$self->{template_paths}}{ @names } ;