The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
[%#
  # IMPORTANT NOTE
  #   This documentation is generated automatically from source
  #   templates.  Any changes you make here may be lost.
  # 
  #   The 'docsrc' documentation source bundle is available for download
  #   from http://www.template-toolkit.org/docs.html and contains all
  #   the source templates, XML files, scripts, etc., from which the
  #   documentation for the Template Toolkit is built.
-%]
[% META book = 'FAQ'
        page = 'FAQ'
%]
[%  WRAPPER toc;
	PROCESS tocitem 
	        title ="DESCRIPTION"
                subs  = [];
	PROCESS tocitem 
	        title ="Template Toolkit Language"
                subs  = [
                    "Why doesn't $tt_start_tag a = b IF c $tt_end_tag work as expected?",
		    "If I'm using TT to write out a TT template, is there a good way to escape $tt_start_tag and $tt_end_tag?",
		    "How do I iterate over a hash?"
		];
	PROCESS tocitem 
	        title ="Plugins"
                subs  = [
                    "How do I get the Table plugin to order data across rather than down?",
		    "Accessing Cookies"
		];
	PROCESS tocitem 
	        title ="Extending the Template Toolkit"
                subs  = [
                    "Can I serve templates from a database?",
		    "Can I fetch templates via http?"
		];
	PROCESS tocitem 
	        title ="Miscellaneous"
                subs  = [
                    "How can I find out the name of the main template being processed?",
		    "How can I find out the name of the current template being processed?",
		    "How do I print the modification time of the template or component?",
		    "How can I configure variables on a per-request basis?"
		];
	PROCESS tocitem 
	        title ="AUTHOR"
                subs  = [];
	PROCESS tocitem 
	        title ="VERSION"
                subs  = [];
	PROCESS tocitem 
	        title ="COPYRIGHT"
                subs  = [];
    END
%]
<!-- Pod to HTML conversion by the Template Toolkit version 2 -->
[% WRAPPER section
    title="DESCRIPTION"
-%][%- END %]
[% WRAPPER section
    title="Template Toolkit Language"
-%][% WRAPPER subsection
   title = "Why doesn't $tt_start_tag a = b IF c $tt_end_tag work as expected?"
-%]<p>
Because the parser interprets it as 
</p>
<pre>    [% tt_start_tag %] a = (b IF c) [% tt_end_tag %]</pre>
<p>
Do this instead:
</p>
<pre>    [% tt_start_tag %] SET a = b IF c [% tt_end_tag %]</pre>
[%- END %]
[% WRAPPER subsection
   title = "If I'm using TT to write out a TT template, is there a good way to escape $tt_start_tag and $tt_end_tag?"
-%]<p>
You can do this:
 
  [% tt_start_tag %] stag = &quot;[\%&quot;
     etag = &quot;%\]&quot;
  [% tt_end_tag %]
 
and then:
 
  [% tt_start_tag %] stag; 'hello'; etag [% tt_end_tag %]
</p>
<p>
Or something like:
</p>
<pre>  [% tt_start_tag %] TAGS [- -] [% tt_end_tag %]
  [- INCLUDE foo -]   # is a directive
  [% tt_start_tag %] INCLUDE foo [% tt_end_tag %]   # not a directive, just plain text, passed through</pre>
[%- END %]
[% WRAPPER subsection
   title = "How do I iterate over a hash?"
-%]<p>
This is covered in the [% ttlink('VMethods', 'Template::Manual::VMethods') -%] section
of the manual page.  A list of all the keys that are in the hash can
be obtained with the 'keys' virtual method.  You can then iterate
over that list and by looking up each key in turn get the value.
</p>
<pre>    [% tt_start_tag %] FOREACH key = product.keys [% tt_end_tag %]
       [% tt_start_tag %] key [% tt_end_tag %] =&gt; [% tt_start_tag %] product.$key [% tt_end_tag %]
    [% tt_start_tag %] END [% tt_end_tag %]</pre>
[%- END %]
[%- END %]
[% WRAPPER section
    title="Plugins"
-%][% WRAPPER subsection
   title = "How do I get the Table plugin to order data across rather than down?"
-%]<p>
Order the data into rows:
</p>
<pre>     Steve     Karen     Jeff
     Brooklyn  Nantucket Fairfax
     NY        MA        VA
 
    [% tt_start_tag %] USE table(data, rows=3) [% tt_end_tag %]
 
Then ask for each column
 
    [% tt_start_tag %] FOREACH column = table.cols [% tt_end_tag %]
 
And then print each item in the column going across the output rows
 
    [% tt_start_tag %] FOREACH item = column [% tt_end_tag %]
	&lt;td&gt;[% tt_start_tag %] item [% tt_end_tag %]&lt;/td&gt;
    [% tt_start_tag %] END [% tt_end_tag %]</pre>
[%- END %]
[% WRAPPER subsection
   title = "Accessing Cookies"
-%]<p>
Jeff Boes &lt;jboes@nexcerpt.com&gt; asks:
</p>
<pre>    Does anyone have a quick-n-dirty approach to accessing 
    cookies from templates? </pre>
<p>
Jonas Liljegren answers:
</p>
<pre>    [% tt_start_tag %] USE CGI [% tt_end_tag %]
    
    &lt;p&gt;The value is [% tt_start_tag %] CGI.cookie('cookie_name') | html [% tt_end_tag %]</pre>
[%- END %]
[%- END %]
[% WRAPPER section
    title="Extending the Template Toolkit"
-%][% WRAPPER subsection
   title = "Can I serve templates from a database?"
-%]<p>
Short answer: yes, Chris Nandor has done this for Slash.  You need to 
subclass Template::Provider.  See the mailing list archives for further
info.
</p>
[%- END %]
[% WRAPPER subsection
   title = "Can I fetch templates via http?"
-%]<p>
To do the job properly, you should sublcass Template::Provider to 
Template::Provider::HTTP and use a PREFIX_MAP option to bind the 
'http' template prefix to that particular provider (you may want to 
go digging around in the <file>Changes</file> file around version 2.01 for 
more info on PREFIX_MAP - it may not be properly documented anywhere
else...yet!).  e.g. (untested due to lack of existing HTTP Provider
- patches welcome!).
</p>
<pre>    use Template::Provider::HTTP;</pre>
<pre>    my $file = Template::Provider( INCLUDE_PATH =&gt; [...] );
    my $http = Template::Provider::HTTP-&gt;new(...);
    my $tt2  = Template-&gt;new({
	LOAD_TEMPLATES =&gt; [ $file, $http ],
	PREFIX_MAP =&gt; {
	    file    =&gt; '0',	# file:foo.html
	    http    =&gt; '1',	# http:foo.html
	    default =&gt; '0',	# foo.html =&gt; file:foo.html
	}
    });</pre>
<p>
Now a template specified as:
</p>
<pre>    [% tt_start_tag %] INCLUDE foo [% tt_end_tag %]</pre>
<p>
will be served by the 'file' provider (the default).  Otherwise you 
can explicitly add a prefix:
</p>
<pre>    [% tt_start_tag %] INCLUDE file:foo.html [% tt_end_tag %]
    [% tt_start_tag %] INCLUDE http:foo.html [% tt_end_tag %]
    [% tt_start_tag %] INCLUDE http://www.xyz.com/tt2/header.tt2 [% tt_end_tag %]</pre>
<p>
This same principal can be used to create a DBI template provider.  e.g.
</p>
<pre>    [% tt_start_tag %] INCLUDE dbi:foo.html [% tt_end_tag %]</pre>
<p>
But similarly, 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...
</p>
<p>
In the mean time, Craig's post from the mailing list has some useful
pointers on how to acheive this using existing modules:
</p>
<pre>    To: Adam Theo &lt;adamtheo@theoretic.com&gt; 
    From: Craig Barratt &lt;craig@arraycomm.com&gt;
    Date: Fri, 18 May 2001 17:06:59 -0700
      
    &gt; i was wondering if there is anyway to fetch a file using http:// or
    &gt; ftp:// and include that?
      
    Here's one way.  Set the LOAD_PERL option:
      
    	use Template;
     
    	my $template = Template-&gt;new({  
    	    LOAD_PERL =&gt; 1
    	});  
    	$template-&gt;process(&quot;example.tt&quot;, { stdout =&gt; *STDOUT })
    				     || die $template-&gt;error();
     
    and then use LWP::UserAgent and HTTP::Request:
     
    	[% tt_start_tag %] 
    	    USE ua = LWP.UserAgent; 
    	    ua.proxy(&quot;http&quot;, &quot;http://your_proxy/&quot;);
    	    USE req = HTTP.Request(&quot;GET&quot;, &quot;http://www.cpan.org&quot;);
    	    ua.request(req).content;
    	-[% tt_end_tag %]
     
    For FTP use Net::FTP:
     
    	[% tt_start_tag %]   
    	    USE ftp = Net.FTP(&quot;ftp.cpan.org&quot;);
    	    x = ftp.login(&quot;anonymous&quot;, &quot;me@here.there&quot;);
    	    x = ftp.cwd(&quot;/&quot;);
    	    x = ftp.get(&quot;welcome.msg&quot;, stdout);
    	    x = ftp.quit;
    	-[% tt_end_tag %]
     
    Normally ftp.get would write the file into the current directory.
    Instead we pass stdout as a second argument so that it is written
    to stdout.  We set stdout to STDOUT in the variables we pass to
    process. 
     
    Craig</pre>
[%- END %]
[%- END %]
[% WRAPPER section
    title="Miscellaneous"
-%][% WRAPPER subsection
   title = "How can I find out the name of the main template being processed?"
-%]<p>
The <code>'template'</code> variable contains a reference to the
Template::Document object for the main template you're processing
(i.e. the one provided as the first argument to the Template process()
method).  The <code>'name'</code> method returns its name.
</p>
<pre>    [% tt_start_tag %] template.name [% tt_end_tag %]     # e.g. index.html</pre>
[%- END %]
[% WRAPPER subsection
   title = "How can I find out the name of the current template being processed?"
-%]<p>
The <code>'template'</code> variable always references the <i>main</i> template being processed.
So even if you call [% tt_start_tag %] INCLUDE header [% tt_end_tag %], and that calls [% tt_start_tag %] INCLUDE menu [% tt_end_tag %],
the <code>'template'</code> variable will be unchanged.
</p>
<p>
index.html:
</p>
<pre>    [% tt_start_tag %] template.name  [% tt_end_tag %]     # index.html
    [% tt_start_tag %] INCLUDE header [% tt_end_tag %]</pre>
<p>
header:
</p>
<pre>    [% tt_start_tag %] template.name  [% tt_end_tag %]     # index.html
    [% tt_start_tag %] INCLUDE menu   [% tt_end_tag %]</pre>
<p>
menu:
</p>
<pre>    [% tt_start_tag %] template.name  [% tt_end_tag %]     # index.html</pre>
<p>
In constrast, the <code>'component'</code> variable always references the <i>current</i>
template being processed.  
</p>
<p>
index.html
</p>
<pre>    [% tt_start_tag %] component.name [% tt_end_tag %]     # index.html
    [% tt_start_tag %] INCLUDE header [% tt_end_tag %]</pre>
<p>
header:
</p>
<pre>    [% tt_start_tag %] component.name [% tt_end_tag %]     # header
    [% tt_start_tag %] INCLUDE menu   [% tt_end_tag %]</pre>
<p>
menu:
</p>
<pre>    [% tt_start_tag %] component.name  [% tt_end_tag %]     # menu</pre>
[%- END %]
[% WRAPPER subsection
   title = "How do I print the modification time of the template or component?"
-%]<p>
The <code>'template'</code> and <code>'component'</code> variables reference the main template
and the current template being processed (see previous questions).
The <code>'modtime'</code> method returns the modification time of the
corresponding template file as a number of seconds since the Unix
epoch (00:00:00 GMT 1st January 1970).
</p>
<p>
This number doesn't mean much to anyone (except perhaps serious Unix
geeks) so you'll probably want to use the Date plugin to format it for
human consumption.
</p>
<pre>    [% tt_start_tag %] USE Date [% tt_end_tag %]</pre>
<pre>    [% tt_start_tag %] template.name [% tt_end_tag %] last modified [% tt_start_tag %] Date.format(template.modtime) [% tt_end_tag %]</pre>
[%- END %]
[% WRAPPER subsection
   title = "How can I configure variables on a per-request basis?"
-%]<p>
One easy way to acheive this is to define a single PRE_PROCESS template which
loads in other configuration files based on variables defined or other 
conditions.
</p>
<p>
For example, my setup usually looks something like this:
</p>
<pre>    PRE_PROCESS =&gt; 'config/main'</pre>
<p>
config/main:
</p>
<pre>    [% tt_start_tag %]  DEFAULT  style   = 'text'
                 section =  template.section or 'home';</pre>
<pre>        PROCESS  config/site
              +  config/urls
              +  config/macros
              + &quot;config/style/$style&quot;
              + &quot;config/section/$section&quot;
              + ...
    [% tt_end_tag %]</pre>
<p>
This allows me to set a single 'style' variable to control which config
file gets pre-processed to set my various style options (colours, img paths,
etc).  For example:
</p>
<p>
config/style/basic:
</p>
<pre>    [% tt_start_tag %]  style = {
	    name = style    # save existing 'style' var as 'style.name'</pre>
<pre>	    # define various other style variables....
            col = {
    		back =&gt; '#ffffff'
	    	text =&gt; '#000000'
		    # ...etc...
	    }</pre>
<pre>	    logo = {
		    # ...etc...
	    }</pre>
<pre>	    # ...etc...
	}
    [% tt_end_tag %]</pre>
<p>
Each source template can declare which section it's in via a META
directive:
</p>
<pre>  [% tt_start_tag %] META
       title   = 'General Information'
       section = 'info'
  [% tt_end_tag %]</pre>
<pre>  ...</pre>
<p>
This controls which section configuration file gets loaded to set various
other variables for defining the section title, menu, etc.
</p>
<p>
config/section/info:
</p>
<pre>    [% tt_start_tag %]  section = {
	        name   = section  # save 'section' var as 'section.name'
	        title  = 'Information'
	        menu   = [ ... ]
	        # ...etc...
	    }
    [% tt_end_tag %]</pre>
<p>
This illustrates the basic principal but you can extend it to perform
pretty much any kind of per-document initialisation that you require.
</p>
[%- END %]
[%- END %]
[% WRAPPER section
    title="AUTHOR"
-%]<p>
Andy Wardley &lt;abw@wardley.org&gt;
</p>
<p>
[% ttlink('http://wardley.org/', 'http://wardley.org/') -%]
</p>
[%- END %]
[% WRAPPER section
    title="VERSION"
-%]<p>
2.68, distributed as part of the
Template Toolkit version 2.19, released on 27 April 2007.
</p>
[%- END %]
[% WRAPPER section
    title="COPYRIGHT"
-%]<pre>  Copyright (C) 1996-2007 Andy Wardley.  All Rights Reserved.</pre>
<p>
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
</p>
[%- END %]