in reply to Template::Simple Help!

You did not indicate what problem you were having, so I assume you have trouble with outputting the 'title' and 'site_name';

This is because your code overwrites '$vars' after setting those values.

To correct it, set all of them at the same time:

my $vars = { title=> = 'Template Test' , site_name => 'TEST Home Page', header => { date => 'Jan 1, 2008', author => 'Me, myself and I', }, row => [ { first => 'row 1 value 1', second => 'row 1 value 2', }, { first => 'row 2 value 1', second => 'row 2 value 2', }, ], footer => { modified => 'Aug 31, 2006', }, } ;
There is nothing wrong with setting the values individually (Except for negligible performance). Just don't overwrite the variable after setting the values.

Another way would be, after setting some values within $var,:

$vars = { %$vars, # Copy in the old values, then add to them.. header => { ...

            "Battle not with trolls, lest ye become a troll; and if you gaze into the Internet, the Internet gazes also into you."
        -Friedrich Nietzsche: A Dynamic Translation

Replies are listed 'Best First'.
Re^2: Template::Simple Help!
by Anonymous Monk on Dec 23, 2011 at 18:37 UTC
    My fault on that example with the '$vars', but yes, my problem is that nothing happens, I doesn't display anything on the screen, I don't even know if the template file is been read by the .pl file.
      After getting this to work, I realize that the documentation is incomplete.

      Here are the things you need to fix, to get this to work:

      • Fix setting $vars as shown in my previous post (Not really REQUIRED)
      • Comment-out "$tmpl->add_templates(" - this pollutes the cache.
      • Change the next line to : $tmpl->compile( 'templ'); # Remove 'demo'
      • Change the 'render' line to : render( 'templ', $vars )
      • print out $$rendered (Note the TWO $'s) (Optional debugging step)
      • In the template '.tmpl' file, Remove the "INCLUDE" for the variables. Include is intended for including other FILEs.
      The 'compile' automatically adds the ".tmpl' suffix, and searches for a file with that name in the 'search_dirs'.

      Here are the last few lines of the code:

      #$tmpl->add_templates( { demo => 'templ.tmpl' } ) ; # Commented OUT $tmpl->compile( 'templ'); #( was 'demo' ) ; my $rendered = $tmpl->render( 'templ', $vars ) ; print $$rendered;
      There are a few other minor issue's - but they will become obvious once your template starts rendering. Happy templating !.

                  "Battle not with trolls, lest ye become a troll; and if you gaze into the Internet, the Internet gazes also into you."
              -Friedrich Nietzsche: A Dynamic Translation

        At least I could see something been printed to the screen, still having issues with the 'header' and 'footer' that is printing HASH(0x1e1b0cc) to the screen. But thanks for looking into this!