home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Syntax.pod < prev    next >
Encoding:
Text File  |  2004-01-30  |  8.2 KB  |  307 lines

  1. #============================================================= -*-perl-*-
  2. #
  3. # Template::Manual::Syntax
  4. #
  5. # DESCRIPTION
  6. #   This section describes the syntax, structure and semantics of the
  7. #   Template Toolkit directives and general presentation language.
  8. #
  9. # AUTHOR
  10. #   Andy Wardley  <abw@andywardley.com>
  11. #
  12. # COPYRIGHT
  13. #   Copyright (C) 1996-2001 Andy Wardley.  All Rights Reserved.
  14. #   Copyright (C) 1998-2001 Canon Research Centre Europe Ltd.
  15. #
  16. #   This module is free software; you can redistribute it and/or
  17. #   modify it under the same terms as Perl itself.
  18. #
  19. # REVISION
  20. #   
  21. #
  22. #========================================================================
  23.  
  24.  
  25. #------------------------------------------------------------------------
  26. # IMPORTANT NOTE
  27. #   This documentation is generated automatically from source
  28. #   templates.  Any changes you make here may be lost.
  29. #   The 'docsrc' documentation source bundle is available for download
  30. #   from http://www.template-toolkit.org/docs.html and contains all
  31. #   the source templates, XML files, scripts, etc., from which the
  32. #   documentation for the Template Toolkit is built.
  33. #------------------------------------------------------------------------
  34.  
  35. =head1 NAME
  36.  
  37. Template::Manual::Syntax - Directive syntax, structure and semantics
  38.  
  39. =head1 DESCRIPTION
  40.  
  41. This section describes the syntax, structure and semantics of the
  42. Template Toolkit directives and general presentation language.
  43.  
  44. =head2 Tag Styles
  45.  
  46. By default, template directives are embedded within the character sequences
  47. '[%' and '%]'.  e.g.
  48.  
  49.     [% PROCESS header %]
  50.   
  51.     <h1>Hello World!</h1>
  52.     <a href="[% page.next %]"><img src="[% icon.next %].gif"></a>
  53.   
  54.     [% PROCESS footer %]
  55.  
  56. You can change the tag characters using the START_TAG, END_TAG and
  57. TAG_STYLE configuration options.  You can also use the TAGS directive
  58. to define a new tag style for the current template file.
  59.  
  60. You can also set the INTERPOLATE option to allow simple variable
  61. references to be embedded directly in templates, prefixed by a '$'.
  62.  
  63.     # INTERPOLATE => 0
  64.     <td>[% name %]</td>  <td>[% email %]</td>
  65.  
  66.     # INTERPOLATE => 1
  67.     <td>$name</td>  <td>$email</td>
  68.  
  69. Directives may be embedded anywhere in a line of text and can be split
  70. across several lines.  Insignificant whitespace is generally ignored
  71. within the directive.
  72.  
  73.     [% INCLUDE header           
  74.        title = 'Hello World' 
  75.        bgcol = '#ffffff' 
  76.     %]
  77.   
  78.     [%INCLUDE menu align='right'%]
  79.   
  80.     Name: [% name %]  ([%id%])
  81.  
  82. =head2 Comments
  83.  
  84. The '#' character is used to indicate comments within a directive.
  85. When placed immediately inside the opening directive tag, it causes
  86. the entire directive to be ignored.
  87.  
  88.     [%# this entire directive is ignored no
  89.         matter how many lines it wraps onto
  90.     %]
  91.  
  92. In any other position, it causes the remainder of the current line to 
  93. be treated as a comment.
  94.  
  95.     [% # this is a comment
  96.        theta = 20      # so is this
  97.        rho   = 30      # <aol>me too!</aol>
  98.     %]
  99.  
  100. =head2 Chomping Whitespace
  101.  
  102. You can add '-' or '+' to the immediate start or end of a directive
  103. tag to control the whitespace chomping options.  See the PRE_CHOMP and
  104. POST_CHOMP options for further details.
  105.  
  106.     [% BLOCK foo -%]        # remove trailing newline
  107.     This is block foo
  108.     [%- END %]            # remove leading newline
  109.  
  110. =head2 Implicit Directives: GET and SET
  111.  
  112. The simplest directives are GET and SET which retrieve and update
  113. variable values respectively.  The GET and SET keywords are actually
  114. optional as the parser is smart enough to see them for what they
  115. really are (but note the caveat below on using side-effect notation).
  116. Thus, you'll generally see:
  117.  
  118.     [% SET foo = 10 %]
  119.     [% GET foo %]
  120.  
  121. written as:
  122.  
  123.     [% foo = 10 %]
  124.     [% foo %]
  125.  
  126. You can also express simple logical statements as implicit GET directives:
  127.  
  128.     [% title or template.title or 'Default Title' %]
  129.  
  130.     [% mode == 'graphics' ? "Graphics Mode Enabled" : "Text Mode" %]
  131.  
  132. All other directives should start with a keyword specified in UPPER
  133. CASE (but see the ANYCASE option).  All directives keywords are in
  134. UPPER CASE to make them visually distinctive and to distinguish them
  135. from variables of the same name but different case.  It is perfectly
  136. valid, for example, to define a variable called 'stop' which is
  137. entirely separate from the STOP directive.
  138.  
  139.     [% stop = 'Clackett Lane Bus Depot' %]
  140.  
  141.     The bus will next stop at [% stop %]    # variable
  142.  
  143.     [% STOP %]                              # directive
  144.  
  145. =head2 Block Directives
  146.  
  147. Directives such as FOREACH, WHILE, BLOCK, FILTER, etc., mark the start
  148. of a block which may contain text or other directives up to the
  149. matching END directive.  Blocks may be nested indefinitely.  The
  150. IF, UNLESS, ELSIF and ELSE directives also define blocks and may be
  151. grouped together in the usual manner.
  152.  
  153.     [% FOREACH item = [ 'foo' 'bar' 'baz' ] %]
  154.        * Item: [% item %]
  155.     [% END %]
  156.   
  157.     [% BLOCK footer %]
  158.        Copyright 2000 [% me %]
  159.        [% INCLUDE company/logo %]
  160.     [% END %]
  161.   
  162.     [% IF foo %]
  163.        [% FOREACH thing = foo.things %]
  164.       [% thing %]
  165.        [% END %]
  166.     [% ELSIF bar %]
  167.        [% INCLUDE barinfo %]
  168.     [% ELSE %]
  169.        do nothing...
  170.     [% END %]
  171.  
  172. Block directives can also be used in a convenient side-effect notation.
  173.  
  174.     [% INCLUDE userinfo FOREACH user = userlist %]
  175.  
  176.     [% INCLUDE debugtxt msg="file: $error.info" 
  177.          IF debugging %] 
  178.  
  179.     [% "Danger Will Robinson" IF atrisk %]
  180.  
  181. versus:
  182.  
  183.     [% FOREACH user = userlist %]
  184.        [% INCLUDE userinfo %]
  185.     [% END %]
  186.  
  187.     [% IF debugging %]
  188.        [% INCLUDE debugtxt msg="file: $error.info" %]
  189.     [% END %]
  190.  
  191.     [% IF atrisk %]
  192.     Danger Will Robinson
  193.     [% END %]
  194.  
  195. =head2 Capturing Block Output
  196.  
  197. The output of a directive can be captured by simply assigning the directive
  198. to a variable.
  199.  
  200.     [% headtext = PROCESS header title="Hello World" %]
  201.  
  202.     [% people = PROCESS userinfo FOREACH user = userlist %]
  203.  
  204. This can be used in conjunction with the BLOCK directive for defining large 
  205. blocks of text or other content.
  206.  
  207.     [% poem = BLOCK %]
  208.        The boy stood on the burning deck,
  209.        His fleece was white as snow.
  210.        A rolling stone gathers no moss,
  211.        And Keith is sure to follow.
  212.     [% END %]
  213.  
  214. Note one important caveat of using this syntax in conjunction with side-effect
  215. notation.  The following directive does not behave as might be expected:
  216.  
  217.     [% var = 'value' IF some_condition %]
  218.  
  219. In this case, the directive is interpreted as (spacing added for clarity)
  220.  
  221.     [% var = IF some_condition %]
  222.        value
  223.     [% END %]
  224.  
  225. rather than
  226.  
  227.     [% IF some_condition %]
  228.        [% var = 'value' %]
  229.     [% END %]
  230.  
  231. The variable is assigned the output of the IF block which returns
  232. 'value' if true, but nothing if false.  In other words, the following
  233. directive will always cause 'var' to be cleared.
  234.  
  235.     [% var = 'value' IF 0 %]
  236.  
  237. To achieve the expected behaviour, the directive should be written as:
  238.  
  239.     [% SET var = 'value' IF some_condition %]
  240.  
  241. =head2 Chaining Filters
  242.  
  243. Multiple FILTER directives can be chained together in sequence.  They
  244. are called in the order defined, piping the output of one into the 
  245. input of the next.
  246.  
  247.     [% PROCESS somefile FILTER truncate(100) FILTER html %]
  248.  
  249. The pipe character, '|', can also be used as an alias for FILTER.
  250.  
  251.     [% PROCESS somefile | truncate(100) | html %]
  252.  
  253. =head2 Multiple Directive Blocks
  254.  
  255. Multiple directives can be included within a single tag when delimited
  256. by semi-colons, ';'.  Note however that the TAGS directive must always
  257. be specified in a tag by itself.
  258.  
  259.     [% IF title; 
  260.           INCLUDE header; 
  261.        ELSE; 
  262.       INCLUDE other/header  title="Some Other Title";
  263.        END
  264.     %]
  265.  
  266. versus
  267.  
  268.     [% IF title %]
  269.        [% INCLUDE header %]
  270.     [% ELSE %]
  271.        [% INCLUDE other/header  title="Some Other Title" %]
  272.     [% END %]
  273.  
  274. =head1 AUTHOR
  275.  
  276. Andy Wardley E<lt>abw@andywardley.comE<gt>
  277.  
  278. L<http://www.andywardley.com/|http://www.andywardley.com/>
  279.  
  280.  
  281.  
  282.  
  283. =head1 VERSION
  284.  
  285. Template Toolkit version 2.13, released on 30 January 2004.
  286.  
  287. =head1 COPYRIGHT
  288.  
  289.   Copyright (C) 1996-2004 Andy Wardley.  All Rights Reserved.
  290.   Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.
  291.  
  292. This module is free software; you can redistribute it and/or
  293. modify it under the same terms as Perl itself.
  294.  
  295.  
  296.  
  297. =cut
  298.  
  299. # Local Variables:
  300. # mode: perl
  301. # perl-indent-level: 4
  302. # indent-tabs-mode: nil
  303. # End:
  304. #
  305. # vim: expandtab shiftwidth=4:
  306.