# perldoc > Template::Tutorial::Web

---
type: CommandReference
command: Template::Tutorial::Web
mode: perldoc
section: 
source: perldoc
---

## Quick Reference

- `[% INCLUDE file %]` — include another template
- `[% SET var = value %]` — assign a variable
- `[% FOREACH item IN list %]... [% END %]` — iterate over list
- `[% IF condition %]...[% ELSE %]...[% END %]` — conditional
- `[% USE plugin %]` — load a plugin (e.g., DBI)
- `tpage template.tt > output.html` — process a single template
- `ttree -f config` — batch process a directory of templates
- `Template->new({ INCLUDE_PATH => [...] })->process($file, $vars)` — CGI/mod_perl usage

## Name

Generating Web Content Using the Template Toolkit

## Synopsis

The Template Toolkit is a Perl module system for processing text templates. Templates use `[% ... %]` tags for variables and directives. Typical usage:

- Static content: `tpage template.tt > output.html`
- Batch processing: `ttree` with configuration file
- Dynamic content: `Template->new({...})->process($file, $vars)` in CGI or mod_perl handlers

## Directives (Key Options)

- `GET` — output a variable value (keyword optional, e.g. `[% var %]`)
- `SET` — assign a variable (keyword optional, e.g. `[% var = value %]`)
- `INCLUDE` — process and include another template file
- `PROCESS` — similar to INCLUDE but processes without local variable scope
- `WRAPPER` — wrap output in a template
- `FOREACH` — iterate over a list, aliasing a loop variable
- `IF`, `UNLESS`, `ELSIF`, `ELSE` — conditionals
- `USE` — load a plugin module

## Examples

### Static page with INCLUDE and FOREACH

html
[% INCLUDE header title = 'Links' %]
<h1>Links</h1>
<ul>
[% FOREACH page IN pages %]
  <li><a href="[% page.url %]">[% page.title %]</a>
[% END %]
</ul>
[% INCLUDE footer %]
### Using tpage

shell
tpage example.tt > example.html
### Using ttree

Configuration file `~/.ttreerc`:

text
verbose
recurse
src = ~/websrc/src
lib = ~/websrc/lib
dest = ~/public_html/test
ignore = \b(CVS|RCS)\b
ignore = ^#
Run:

shell
ttree
### CGI script

perl
#!/usr/bin/perl
use strict;
use warnings;
use Template;
use CGI;

print "Content-type: text/html\n\n";
my $file = 'userinfo.html';
my $vars = {
    version => 3.14,
    days    => [ qw(mon tue wed thu fri sat sun) ],
    cgi     => CGI->new(),
    me      => { id => 'abw', name => 'Andy Wardley' },
};
my $template = Template->new({
    INCLUDE_PATH => '/home/abw/websrc/src:/home/abw/websrc/lib',
    PRE_PROCESS  => 'config',
});
$template->process($file, $vars) || die $template->error();
### Apache/mod_perl handler

perl
package MyOrg::Apache::User;
use strict;
use Apache::Constants qw(:common);
use Template;
use CGI;

sub handler {
    my $r = shift;
    my $websrc = $r->dir_config('websrc_root')
        or return fail($r, SERVER_ERROR, "'websrc_root' not specified");
    my $template = Template->new({
        INCLUDE_PATH => "$websrc/src/user:$websrc/lib",
        PRE_PROCESS  => 'config',
        OUTPUT       => $r,
    });
    my $params = { uri => $r->uri, cgi => CGI->new };
    my $file = $r->path_info;
    $file =~ s[^/][];
    $r->content_type('text/html');
    $r->send_http_header;
    $template->process($file, $params) || return fail($r, SERVER_ERROR, $template->error());
    return OK;
}
sub fail { ... }
### Using a plugin (DBI)

html
[% INCLUDE header title = 'User Info' %]
[% USE DBI('dbi:mSQL:mydbname') %]
<table>
  <tr><th>User ID</th><th>Name</th><th>Email</th></tr>
[% FOREACH user IN DBI.query('SELECT * FROM user ORDER BY id') %]
  <tr><td>[% user.id %]</td><td>[% user.name %]</td><td>[% user.email %]</td></tr>
[% END %]
</table>
[% INCLUDE footer %]
## See Also

- [Template](http://localhost/phpMan.php/perldoc/Template) — module usage
- [Template::Manual](http://localhost/phpMan.php/perldoc/Template::Manual) — index to manual
- [Template::Manual::Config](http://localhost/phpMan.php/perldoc/Template::Manual::Config) — configuration options
- [Template::Tools::ttree](http://localhost/phpMan.php/perldoc/Template::Tools::ttree) — batch processing tool
- [Apache::Template](http://localhost/phpMan.php/perldoc/Apache::Template) — mod_perl interface
- [Template::Plugin](http://localhost/phpMan.php/perldoc/Template::Plugin) — plugin base class