Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Just a couple little things. About caching: TT2 compiles a template to a Perl subroutine and caches it, which is very fast indeed! It also allows you to save this Perl subroutine to disk so the next time a system like mod_perl starts up you don't even have to parse the template. Sweet! (Yes, I'm a big fan of TT.)

Another nifty benefit that you touched on briefly is the consistent notation for variable access. For instance you can start out a template using a hash user:

my $tmpl = Template->new(); $tmpl->process( 'user_form.tmpl', { user => { name => 'Bruce Springsteen', last_login => '2000-01-01 05:45:00' }, common_date_format => \&my_date_format } );

user_form.tmpl

<p>Welcome [% user.name %]. Your last login was [% common_date_format( user.last_login ) %].</p>

Later, you might want to make user an object which has the methods name() and last_login():

my $user = User->fetch( 'theboss' ); my $tmpl = Template->new(); $tmpl->process( 'user_form.tmpl', { user => $user, common_date_format => \&my_date_format } );

This requires no modification to your template, which makes refactoring a larger system a more palatable task.

Chris
M-x auto-bs-mode


In reply to Re: Template Toolkit 2 by lachoy
in thread Template Toolkit 2 by Masem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2023-09-26 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?