[%# # 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 = 'Manual' page = 'Syntax' %] [% WRAPPER toc; INCLUDE tocitem title='DESCRIPTION' subs=['Tag Styles', 'Comments', 'Chomping Whitespace', 'Implicit Directives: GET and SET', 'Block Directives', 'Capturing Block Output', 'Chaining Filters', 'Multiple Directive Blocks']; INCLUDE tocitem title='AUTHOR'; INCLUDE tocitem title='VERSION'; INCLUDE tocitem title='COPYRIGHT'; END %] [% WRAPPER section title='DESCRIPTION' -%]

This section describes the syntax, structure and semantics of the Template Toolkit directives and general presentation language.

[% WRAPPER subsection title = 'Tag Styles' -%]

By default, template directives are embedded within the character sequences '[% tt_start_tag %]' and '[% tt_end_tag %]'. e.g.

        [% tt_start_tag %] PROCESS header [% tt_end_tag %]

        <h1>Hello World!</h1>
        <a href="[% tt_start_tag %] page.next [% tt_end_tag %]"><img src="[% tt_start_tag %] icon.next [% tt_end_tag %].gif"></a>

        [% tt_start_tag %] PROCESS footer [% tt_end_tag %]

You can change the tag characters using the START_TAG, END_TAG and TAG_STYLE configuration options. You can also use the TAGS directive to define a new tag style for the current template file.

You can also set the INTERPOLATE option to allow simple variable references to be embedded directly in templates, prefixed by a '$'.

        # INTERPOLATE => 0
        <td>[% tt_start_tag %] name [% tt_end_tag %]</td>  <td>[% tt_start_tag %] email [% tt_end_tag %]</td>

        # INTERPOLATE => 1
        <td>$name</td>  <td>$email</td>

Directives may be embedded anywhere in a line of text and can be split across several lines. Insignificant whitespace is generally ignored within the directive.

        [% tt_start_tag %] INCLUDE header
           title = 'Hello World'
           bgcol = '#ffffff'
        [% tt_end_tag %]

        [% tt_start_tag %]INCLUDE menu align='right'[% tt_end_tag %]

        Name: [% tt_start_tag %] name [% tt_end_tag %]  ([% tt_start_tag %]id[% tt_end_tag %])

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

The '#' character is used to indicate comments within a directive. When placed immediately inside the opening directive tag, it causes the entire directive to be ignored.

        [% tt_start_tag %]# this entire directive is ignored no
            matter how many lines it wraps onto
        [% tt_end_tag %]

In any other position, it causes the remainder of the current line to be treated as a comment.

        [% tt_start_tag %] # this is a comment
           theta = 20      # so is this
           rho   = 30      # <aol>me too!</aol>
        [% tt_end_tag %]

[%- END %] [% WRAPPER subsection title = 'Chomping Whitespace' -%]

You can add '-' or '+' to the immediate start or end of a directive tag to control the whitespace chomping options. See the PRE_CHOMP and POST_CHOMP options for further details.

        [% tt_start_tag %] BLOCK foo -[% tt_end_tag %]		# remove trailing newline
        This is block foo
        [% tt_start_tag %]- END [% tt_end_tag %]			# remove leading newline

[%- END %] [% WRAPPER subsection title = 'Implicit Directives: GET and SET' -%]

The simplest directives are GET and SET which retrieve and update variable values respectively. The GET and SET keywords are actually optional as the parser is smart enough to see them for what they really are (but note the caveat below on using side-effect notation). Thus, you'll generally see:

        [% tt_start_tag %] SET foo = 10 [% tt_end_tag %]
        [% tt_start_tag %] GET foo [% tt_end_tag %]

written as:

        [% tt_start_tag %] foo = 10 [% tt_end_tag %]
        [% tt_start_tag %] foo [% tt_end_tag %]

You can also express simple logical statements as implicit GET directives:

        [% tt_start_tag %] title or template.title or 'Default Title' [% tt_end_tag %]

        [% tt_start_tag %] mode == 'graphics' ? "Graphics Mode Enabled" : "Text Mode" [% tt_end_tag %]

All other directives should start with a keyword specified in UPPER CASE (but see the ANYCASE option). All directives keywords are in UPPER CASE to make them visually distinctive and to distinguish them from variables of the same name but different case. It is perfectly valid, for example, to define a variable called 'stop' which is entirely separate from the STOP directive.

        [% tt_start_tag %] stop = 'Clackett Lane Bus Depot' [% tt_end_tag %]

        The bus will next stop at [% tt_start_tag %] stop [% tt_end_tag %]    # variable

        [% tt_start_tag %] STOP [% tt_end_tag %]                              # directive

[%- END %] [% WRAPPER subsection title = 'Block Directives' -%]

Directives such as FOREACH, WHILE, BLOCK, FILTER, etc., mark the start of a block which may contain text or other directives up to the matching END directive. Blocks may be nested indefinitely. The IF, UNLESS, ELSIF and ELSE directives also define blocks and may be grouped together in the usual manner.

        [% tt_start_tag %] FOREACH item = [ 'foo' 'bar' 'baz' ] [% tt_end_tag %]
           * Item: [% tt_start_tag %] item [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] BLOCK footer [% tt_end_tag %]
           Copyright 2000 [% tt_start_tag %] me [% tt_end_tag %]
           [% tt_start_tag %] INCLUDE company/logo [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] IF foo [% tt_end_tag %]
           [% tt_start_tag %] FOREACH thing = foo.things [% tt_end_tag %]
    	  [% tt_start_tag %] thing [% tt_end_tag %]
           [% tt_start_tag %] END [% tt_end_tag %]
        [% tt_start_tag %] ELSIF bar [% tt_end_tag %]
           [% tt_start_tag %] INCLUDE barinfo [% tt_end_tag %]
        [% tt_start_tag %] ELSE [% tt_end_tag %]
           do nothing...
        [% tt_start_tag %] END [% tt_end_tag %]

Block directives can also be used in a convenient side-effect notation.

        [% tt_start_tag %] INCLUDE userinfo FOREACH user = userlist [% tt_end_tag %]

        [% tt_start_tag %] INCLUDE debugtxt msg="file: $error.info"
             IF debugging [% tt_end_tag %]

        [% tt_start_tag %] "Danger Will Robinson" IF atrisk [% tt_end_tag %]

versus:

        [% tt_start_tag %] FOREACH user = userlist [% tt_end_tag %]
           [% tt_start_tag %] INCLUDE userinfo [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] IF debugging [% tt_end_tag %]
           [% tt_start_tag %] INCLUDE debugtxt msg="file: $error.info" [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] IF atrisk [% tt_end_tag %]
        Danger Will Robinson
        [% tt_start_tag %] END [% tt_end_tag %]

[%- END %] [% WRAPPER subsection title = 'Capturing Block Output' -%]

The output of a directive can be captured by simply assigning the directive to a variable.

        [% tt_start_tag %] headtext = PROCESS header title="Hello World" [% tt_end_tag %]

        [% tt_start_tag %] people = PROCESS userinfo FOREACH user = userlist [% tt_end_tag %]

This can be used in conjunction with the BLOCK directive for defining large blocks of text or other content.

        [% tt_start_tag %] poem = BLOCK [% tt_end_tag %]
           The boy stood on the burning deck,
           His fleece was white as snow.
           A rolling stone gathers no moss,
           And Keith is sure to follow.
        [% tt_start_tag %] END [% tt_end_tag %]

Note one important caveat of using this syntax in conjunction with side-effect notation. The following directive does not behave as might be expected:

        [% tt_start_tag %] var = 'value' IF some_condition [% tt_end_tag %]

In this case, the directive is interpreted as (spacing added for clarity)

        [% tt_start_tag %] var = IF some_condition [% tt_end_tag %]
           value
        [% tt_start_tag %] END [% tt_end_tag %]

rather than

        [% tt_start_tag %] IF some_condition [% tt_end_tag %]
           [% tt_start_tag %] var = 'value' [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

The variable is assigned the output of the IF block which returns 'value' if true, but nothing if false. In other words, the following directive will always cause 'var' to be cleared.

        [% tt_start_tag %] var = 'value' IF 0 [% tt_end_tag %]

To achieve the expected behaviour, the directive should be written as:

        [% tt_start_tag %] SET var = 'value' IF some_condition [% tt_end_tag %]

[%- END %] [% WRAPPER subsection title = 'Chaining Filters' -%]

Multiple FILTER directives can be chained together in sequence. They are called in the order defined, piping the output of one into the input of the next.

        [% tt_start_tag %] PROCESS somefile FILTER truncate(100) FILTER html [% tt_end_tag %]

The pipe character, '|', can also be used as an alias for FILTER.

        [% tt_start_tag %] PROCESS somefile | truncate(100) | html [% tt_end_tag %]

[%- END %] [% WRAPPER subsection title = 'Multiple Directive Blocks' -%]

Multiple directives can be included within a single tag when delimited by semi-colons, ';'. Note however that the TAGS directive must always be specified in a tag by itself.

        [% tt_start_tag %] IF title;
              INCLUDE header;
           ELSE;
    	  INCLUDE other/header  title="Some Other Title";
           END
        [% tt_end_tag %]

versus

        [% tt_start_tag %] IF title [% tt_end_tag %]
           [% tt_start_tag %] INCLUDE header [% tt_end_tag %]
        [% tt_start_tag %] ELSE [% tt_end_tag %]
           [% tt_start_tag %] INCLUDE other/header  title="Some Other Title" [% tt_end_tag %]
        [% tt_start_tag %] END [% tt_end_tag %]

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

Template Toolkit version 2.01, released on 30th March 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 %]