[%# # 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; INCLUDE tocitem title = 'DESCRIPTION'; INCLUDE tocitem title = 'General Questions' subs = [ 'Why is the sky blue?' ]; INCLUDE tocitem title = 'Template Toolkit Language' subs = [ 'Why doesn\'t $tt_start_tag a = b IF c $tt_end_tag work as expected?' ]; INCLUDE tocitem title = 'Plugins' subs = [ 'How do I get the Table plugin to order data across rather than down?', 'Accessing Cookies' ]; INCLUDE tocitem title = 'Extending the Template Toolkit' subs = [ 'Can I serve templates from a database?', 'Can I fetch templates via http?', 'Miscellaneous' ]; INCLUDE tocitem title = 'AUTHOR'; INCLUDE tocitem title = 'VERSION'; INCLUDE tocitem title = 'COPYRIGHT'; END %] [% WRAPPER section title='DESCRIPTION' -%]

This is the Frequently Asked Questions list for the Template Toolkit. More accurately, it's a very thin placeholder for where the FAQ will soon be.

[%- END %] [% WRAPPER section title='General Questions' -%][% WRAPPER subsection title = 'Why is the sky blue?' -%]

Something to do with refraction.

[%- END %] [%- 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?' -%]

Because the parser interprets it as

        [% tt_start_tag %] a = (b IF c) [% tt_end_tag %]

Do this instead:

        [% tt_start_tag %] SET a = b IF c [% tt_end_tag %]

[%- END %] [%- END %] [% WRAPPER section title='Plugins' -%][% WRAPPER subsection title = '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

        [% 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 %]
    	<td>[% tt_start_tag %] item [% tt_end_tag %]</td>
        [% tt_start_tag %] END [% tt_end_tag %]

[%- END %] [% WRAPPER subsection title = 'Accessing Cookies' -%]

Jeff Boes <boes@nexcerpt.com> writes:

        Does anyone have a quick-n-dirty approach to accessing
        cookies from templates?

Jonas Liljegren replies:

        [% tt_start_tag %] USE CGI [% tt_end_tag %]

        <p>The value is [% tt_start_tag %] CGI.cookie('cookie_name') | html [% tt_end_tag %]

[%- END %] [%- END %] [% WRAPPER section title='Extending the Template Toolkit' -%][% WRAPPER subsection title = 'Can I serve templates from a database?' -%]

Short answer: yes, Chris Nandor has done this for Slash. You need to subclass Template::Provider. See the mailing list archives for further info.

[%- END %] [% WRAPPER subsection title = 'Can I fetch templates via http?' -%]

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 Changes 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!).

        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:

        [% tt_start_tag %] INCLUDE foo [% tt_end_tag %]

will be served by the 'file' provider (the default). Otherwise you can explicitly add a prefix:

        [% 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 %]

This same principal can be used to create a DBI template provider. e.g.

        [% tt_start_tag %] INCLUDE dbi:foo.html [% tt_end_tag %]

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...

In the mean time, Craig's post from the mailing list has some useful pointers on how to acheive this using existing modules:

        To: Adam Theo <adamtheo@theoretic.com>
        From: Craig Barratt <craig@arraycomm.com>
        Date: Fri, 18 May 2001 17:06:59 -0700

        > i was wondering if there is anyway to fetch a file using http:// or
        > ftp:// and include that?

        Here's one way.  Set the LOAD_PERL option:

        	use Template;

        	my $template = Template->new({
        	    LOAD_PERL => 1
        	});
        	$template->process("example.tt", { stdout => *STDOUT })
        				     || die $template->error();

        and then use LWP::UserAgent and HTTP::Request:

        	[% tt_start_tag %]
        	    USE ua = LWP.UserAgent;
        	    ua.proxy("http", "http://your_proxy/");
        	    USE req = HTTP.Request("GET", "http://www.cpan.org");
        	    ua.request(req).content;
        	-[% tt_end_tag %]

        For FTP use Net::FTP:

        	[% tt_start_tag %]
        	    USE ftp = Net.FTP("ftp.cpan.org");
        	    x = ftp.login("anonymous", "me@here.there");
        	    x = ftp.cwd("/");
        	    x = ftp.get("welcome.msg", 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

[%- END %] [% WRAPPER subsection title = 'Miscellaneous' -%]

- Can I have another cup of coffee?

  No, I think you've hhhaadd ennnough alllrready.

[%- END %] [%- END %] [% WRAPPER section title='AUTHOR' -%]

Andy Wardley <abw@kfs.org>

[% ttlink('http://www.andywardley.com/', 'http://www.andywardley.com/') -%]

[%- END %] [% WRAPPER section title='VERSION' -%]

2.30, distributed as part of the Template Toolkit version 2.06, released on 07 November 2001.

[%- END %] [% WRAPPER section title='COPYRIGHT' -%]

      Copyright (C) 1996-2001 Andy Wardley.  All Rights Reserved.
      Copyright (C) 1998-2001 Canon Research Centre Europe Ltd.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

[%- END %]