Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there Monks!
I am trying to use this template system that its supposed to be simple and light, but can't get this simple example to work, I am wondering if anyone here has done any work using this template module or show me please where I am going wrong on my example here:
temp.pl
#!/usr/bin/perl -w use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use Template::Simple; my $q = new CGI; #my $tmpl = Template::Simple->new(); my $tmpl = Template::Simple->new( search_dirs => [ ( '/dir') ], ); print header(); my $vars = {}; $vars->{'title'} = 'Template Test'; $vars->{'site_name'} = 'TEST Home Page'; $vars = { 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', }, } ; #my $rendered = $tmpl->render( 'templ.tmpl', $vars )|| die $tmpl->erro +r(); $tmpl->add_templates( { demo => 'templ.tmpl' } ) ; $tmpl->compile( 'demo' ) ; my $rendered = $tmpl->render( 'demo', $vars ) ;

temp.tmpl
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http:// +www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd" <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-type" content="application/xhtml+xml; charse +t=utf-8" /> <meta http-equiv="Content-language" content="en-gb" /> <meta name="viewport" content="width=device-width; initial-scale=1.0; +maximum-scale=2.5; user-scalable=1;" /> <title>Temp Test [% title %]</title> </head> <body> <h3>Template Simple</h3> <p><b>[%include title %]</b></p> <br> <p><b>[%INCLUDE site_name%]</b></p> <br><br> [%INCLUDE header%] [%START row%] [%first%] - [%second%] [%END row%] [%INCLUDE footer%] </body> </html>
Thanks for looking!

Replies are listed 'Best First'.
Re: Template::Simple Help!
by NetWallah (Canon) on Dec 23, 2011 at 16:48 UTC
    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

      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