asks:
.PP
.Vb 2
\& Does anyone have a quick\-n\-dirty approach to accessing
\& cookies from templates?
.Ve
.PP
Jonas Liljegren answers:
.PP
.Vb 1
\& [% USE CGI %]
\&
\& The value is [% CGI.cookie(\*(Aqcookie_name\*(Aq) | html %]
.Ve
.PP
You will need to have Template::Plugin::CGI installed.
.SH "Extending the Template Toolkit"
.IX Header "Extending the Template Toolkit"
.SS "Can I serve templates from a database?"
.IX Subsection "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.
.SS "Can I fetch templates via http?"
.IX Subsection "Can I fetch templates via http?"
To do the job properly, you should subclass Template::Provider to
\&\f(CW\*(C`Template::Provider::HTTP\*(C'\fR and use a \f(CW\*(C`PREFIX_MAP\*(C'\fR option to bind the \f(CW\*(C`http\*(C'\fR
template prefix to that particular provider (you may want to go digging around
in the \fIChanges\fR file around version 2.01 for more info on \f(CW\*(C`PREFIX_MAP\*(C'\fR \- it
may not be properly documented anywhere else...yet!). e.g.
.PP
.Vb 1
\& 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 => \*(Aq0\*(Aq, # file:foo.html
\& http => \*(Aq1\*(Aq, # http:foo.html
\& default => \*(Aq0\*(Aq, # foo.html => file:foo.html
\& }
\& });
.Ve
.PP
Now a template specified as:
.PP
.Vb 1
\& [% INCLUDE foo %]
.Ve
.PP
will be served by the 'file' provider (the default). Otherwise you
can explicitly add a prefix:
.PP
.Vb 3
\& [% INCLUDE file:foo.html %]
\& [% INCLUDE http:foo.html %]
\& [% INCLUDE http://www.xyz.com/tt2/header.tt2 %]
.Ve
.PP
This same principal can be used to create a \s-1DBI\s0 template provider. e.g.
.PP
.Vb 1
\& [% INCLUDE dbi:foo.html %]
.Ve
.PP
Alas, we don't yet have a \s-1DBI\s0 provider as part of the Template Toolkit. There
has been some talk on the mailing list about efforts to develop \s-1DBI\s0 and/or
\&\s-1HTTP\s0 providers but as yet no-one has stepped forward to take up the
challenge...
.PP
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
.SH "Miscellaneous"
.IX Header "Miscellaneous"
.SS "How can I find out the name of the main template being processed?"
.IX Subsection "How can I find out the name of the main template being processed?"
The \f(CW\*(C`template\*(C'\fR 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 \fBprocess()\fR
method). The \f(CW\*(C`name\*(C'\fR method returns its name.
.PP
.Vb 1
\& [% template.name %] # e.g. index.html
.Ve
.SS "How can I find out the name of the current template being processed?"
.IX Subsection "How can I find out the name of the current template being processed?"
The \f(CW\*(C`template\*(C'\fR variable always references the \fImain\fR template being processed.
So even if you call [% \s-1INCLUDE\s0 header %], and that calls [% \s-1INCLUDE\s0 menu %],
the \f(CW\*(C`template\*(C'\fR variable will be unchanged.
.PP
index.html:
.PP
.Vb 2
\& [% template.name %] # index.html
\& [% INCLUDE header %]
.Ve
.PP
header:
.PP
.Vb 2
\& [% template.name %] # index.html
\& [% INCLUDE menu %]
.Ve
.PP
menu:
.PP
.Vb 1
\& [% template.name %] # index.html
.Ve
.PP
In contrast, the \f(CW\*(C`component\*(C'\fR variable always references the \fIcurrent\fR
template being processed.
.PP
index.html
.PP
.Vb 2
\& [% component.name %] # index.html
\& [% INCLUDE header %]
.Ve
.PP
header:
.PP
.Vb 2
\& [% component.name %] # header
\& [% INCLUDE menu %]
.Ve
.PP
menu:
.PP
.Vb 1
\& [% component.name %] # menu
.Ve
.SS "How do I print the modification time of the template or component?"
.IX Subsection "How do I print the modification time of the template or component?"
The \f(CW\*(C`template\*(C'\fR and \f(CW\*(C`component\*(C'\fR variables reference the main template
and the current template being processed (see previous questions).
The \f(CW\*(C`modtime\*(C'\fR method returns the modification time of the
corresponding template file as a number of seconds since the Unix
epoch (00:00:00 \s-1GMT\s0 1st January 1970).
.PP
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.
.PP
.Vb 2
\& [% USE Date %]
\& [% template.name %] last modified [% Date.format(template.modtime) %]
.Ve
.SS "How can I configure variables on a per-request basis?"
.IX Subsection "How can I configure variables on a per-request basis?"
One easy way to achieve this is to define a single \f(CW\*(C`PRE_PROCESS\*(C'\fR template
which loads in other configuration files based on variables defined or other
conditions.
.PP
For example, my setup usually looks something like this:
.PP
.Vb 1
\& PRE_PROCESS => \*(Aqconfig/main\*(Aq
.Ve
.PP
config/main:
.PP
.Vb 2
\& [% DEFAULT style = \*(Aqtext\*(Aq
\& section = template.section or \*(Aqhome\*(Aq;
\&
\& PROCESS config/site
\& + config/urls
\& + config/macros
\& + "config/style/$style"
\& + "config/section/$section"
\& + ...
\& %]
.Ve
.PP
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:
.PP
config/style/basic:
.PP
.Vb 2
\& [% style = {
\& name = style # save existing \*(Aqstyle\*(Aq var as \*(Aqstyle.name\*(Aq
\&
\& # define various other style variables....
\& col = {
\& back => \*(Aq#ffffff\*(Aq
\& text => \*(Aq#000000\*(Aq
\& # ...etc...
\& }
\&
\& logo = {
\& # ...etc...
\& }
\&
\& # ...etc...
\& }
\& %]
.Ve
.PP
Each source template can declare which section it's in via a \s-1META\s0
directive:
.PP
.Vb 5
\& [% META
\& title = \*(AqGeneral Information\*(Aq
\& section = \*(Aqinfo\*(Aq
\& %]
\& ...
.Ve
.PP
This controls which section configuration file gets loaded to set various
other variables for defining the section title, menu, etc.
.PP
config/section/info:
.PP
.Vb 7
\& [% section = {
\& name = section # save \*(Aqsection\*(Aq var as \*(Aqsection.name\*(Aq
\& title = \*(AqInformation\*(Aq
\& menu = [ ... ]
\& # ...etc...
\& }
\& %]
.Ve
.PP
This illustrates the basic principal but you can extend it to perform
pretty much any kind of per-document initialisation that you require.
.SS "Why do I get rubbish for my utf\-8 templates?"
.IX Subsection "Why do I get rubbish for my utf-8 templates?"
First of all, make sure that your template files define a Byte Order
Mark
.PP
If you for some reason don't want to add \s-1BOM\s0 to your templates, you can
force Template to use a particular encoding (e.g. \f(CW\*(C`utf8\*(C'\fR) for your
templates with the \f(CW\*(C`ENCODING\*(C'\fR option.
.PP
.Vb 3
\& my $template = Template\->new({
\& ENCODING => \*(Aqutf8\*(Aq
\& });
.Ve
.SH "Questions About This FAQ"
.IX Header "Questions About This FAQ"
.SS "Why is this \s-1FAQ\s0 so short?"
.IX Subsection "Why is this FAQ so short?"
Because we don't have anyone maintaining it.
.SS "Can I help?"
.IX Subsection "Can I help?"
Yes please :\-)