#============================================================= -*-perl-*- # # Template::FAQ # # DESCRIPTION # # AUTHOR # Andy Wardley # # COPYRIGHT # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved. # # This module is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. #======================================================================== =head1 NAME Template::FAQ - Frequently Asked Questions about the Template Toolkit =head1 Template Toolkit Language =head2 Why doesn't [% a = b IF c %] work as expected? There's a limitation in the TT2 parser which means that the following code doesn't work as you might expect: [% a = b IF c %] The parser interprets it as an attempt to set C to the result of C, like this: [% a = (b IF c) %] If you want to set C only if C is true, then do this instead: [% SET a = b IF c %] The explicit C keyword gives the parser the clue it needs to do the right thing. NOTE: this will be fixed in TT3 =head2 If I'm using TT to write out a TT template, is there a good way to escape [% and %]? You can do something like this: [% stag = "[\%" etag = "%\]" %] and then: [% stag; 'hello'; etag %] Or you can use the C directive, like so: [% TAGS [- -] %] [- INCLUDE foo -] # is a directive [% INCLUDE foo %] # not a directive =head2 How do I iterate over a hash? This is covered in the L section of the manual. A list of all the keys that are in the hash can be obtained with the C virtual method. You can then iterate over that list and by looking up each key in turn get the value. [% FOREACH key = product.keys %] [% key %] => [% product.$key %] [% END %] =head1 Plugins =head2 How do I get the Table plugin to order data across rather than down? Order the data into rows: Steve Karen Jeff Brooklyn Nantucket Fairfax NY MA VA [% USE table(data, rows=3) %] Then ask for each column [% FOREACH column = table.cols %] And then print each item in the column going across the output rows [% FOREACH item = column %] [% item %] [% END %] =head2 Accessing Cookies Jeff Boes Ejboes@nexcerpt.comE asks: Does anyone have a quick-n-dirty approach to accessing cookies from templates? Jonas Liljegren answers: [% USE CGI %]

The value is [% CGI.cookie('cookie_name') | html %] You will need to have L installed. =head1 Extending the Template Toolkit =head2 Can I serve templates from a database? Short answer: yes, Chris Nandor has done this for Slash. You need to subclass L. See the mailing list archives for further info. =head2 Can I fetch templates via http? To do the job properly, you should subclass L to C and use a C option to bind the C template prefix to that particular provider (you may want to go digging around in the F file around version 2.01 for more info on C - it may not be properly documented anywhere else...yet!). e.g. use Template::Provider::HTTP; my $file = Template::Provider( INCLUDE_PATH => [...] ); my $http = Template::Provider::HTTP->new(...); my $tt2 = Template->new({ LOAD_TEMPLATES => [ $file, $http ], PREFIX_MAP => { file => '0', # file:foo.html http => '1', # http:foo.html default => '0', # foo.html => file:foo.html } }); Now a template specified as: [% INCLUDE foo %] will be served by the 'file' provider (the default). Otherwise you can explicitly add a prefix: [% INCLUDE file:foo.html %] [% INCLUDE http:foo.html %] [% INCLUDE http://www.xyz.com/tt2/header.tt2 %] This same principal can be used to create a DBI template provider. e.g. [% INCLUDE dbi:foo.html %] Alas, we don't yet have a DBI provider as part of the Template Toolkit. There has been some talk on the mailing list about efforts to develop DBI and/or HTTP providers but as yet no-one has stepped forward to take up the challenge... In the mean time, Craig Barrat's post from the mailing list has some useful pointers on how to achieve this using existing modules. See L =head1 Miscellaneous =head2 How can I find out the name of the main template being processed? The C