Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 8b32d38f0094104a5fd847636983daa8 > files > 33

zope-doc-2.11.2-11mdv2010.0.i586.rpm

<html>
<head>
<title>Appendix C: Zope Page Templates Reference</title>
</head>
<body bgcolor="#FFFFFF">
<h1>Appendix C: Zope Page Templates Reference</h1>
<p>  Zope Page Templates are an HTML/XML generation tool. This appendix
  is a reference to Zope Page Templates standards: Tag Attribute
  Language (TAL), TAL Expression Syntax (TALES), and Macro Expansion
  TAL (METAL).</p><h2>  TAL Overview</h2>
<p>    The <em>Template Attribute Language</em> (TAL) standard is an attribute
    language used to create dynamic templates.  It allows elements of a
    document to be replaced, repeated, or omitted.</p><p>    The statements of TAL are XML attributes from the TAL namespace.
    These attributes can be applied to an XML or HTML document in order
    to make it act as a template.</p><p>    A <strong>TAL statement</strong> has a name (the attribute name) and a body (the
    attribute value).  For example, an <code>content</code> statement might look
    like <code>tal:content=&quot;string:Hello&quot;</code>.  The element on which a statement
    is defined is its <strong>statement element</strong>.  Most TAL statements
    require expressions, but the syntax and semantics of these
    expressions are not part of TAL. TALES is recommended for this
    purpose.</p><h3>    TAL Namespace</h3>
<p>      The TAL namespace URI and recommended alias are currently defined
      as:<pre>        xmlns:tal=&quot;http://xml.zope.org/namespaces/tal&quot;</pre>
</p><p>      This is not a URL, but merely a unique identifier.  Do not expect
      a browser to resolve it successfully.</p><p>      Zope does not require an XML namespace declaration when creating
      templates with a content-type of <code>text/html</code>. However, it does
      require an XML namespace declaration for all other content-types.</p><h3>    TAL Statements   </h3>
<p>      These are the tal statements:</p><ul>
<li>tal:attributes - dynamically change
        element attributes.</li>
<li>tal:define - define variables.</li>
<li>tal:condition - test conditions.</li>
<li>tal:content - replace the content of an
        element.</li>
<li>tal:omit-tag - remove an element, leaving
        the content of the element.</li>
<li>tal:on-error - handle errors.</li>
<li>tal:repeat - repeat an element.</li>
<li>tal:replace - replace the content of an
        element and remove the element leaving the content.</li>
</ul>
<p>      Expressions used in statements may return values of any type,
      although most statements will only accept strings, or will convert
      values into a string representation.  The expression language must
      define a value named <em>nothing</em> that is not a string.  In
      particular, this value is useful for deleting elements or
      attributes.</p><h3>    Order of Operations</h3>
<p>      When there is only one TAL statement per element, the order in
      which they are executed is simple.  Starting with the root
      element, each element's statements are executed, then each of its
      child elements is visited, in order, to do the same.</p><p>      Any combination of statements may appear on the same elements,
      except that the <code>content</code> and <code>replace</code> statements may not appear
      together.</p><p>      When an element has multiple statements, they are executed in this
      order:</p><ol>
<li> <code>define</code></li>
<li> <code>condition</code></li>
<li> <code>repeat</code></li>
<li> <code>content</code> or <code>replace</code></li>
<li> <code>attributes</code></li>
<li> <code>omit-tag</code></li>
</ol>
<p>      Since the <code>on-error</code> statement is only invoked when an error occurs,
      it does not appear in the list.  </p><p>      The reasoning behind this ordering goes like this: You often want
      to set up variables for use in other statements, so <code>define</code> comes
      first.  The very next thing to do is decide whether this element
      will be included at all, so <code>condition</code> is next; since the
      condition may depend on variables you just set, it comes after
      <code>define</code>.  It is valuable be able to replace various parts of an
      element with different values on each iteration of a repeat, so
      <code>repeat</code> is next.  It makes no sense to replace attributes and
      then throw them away, so <code>attributes</code> is last.  The remaining
      statements clash, because they each replace or edit the statement
      element.</p><h3>    See Also</h3>
<p>      TALES Overview</p><p>      METAL Overview</p><p>      tal:attributes</p><p>      tal:define</p><p>      tal:condition</p><p>      tal:content</p><p>      tal:omit-tag</p><p>      tal:on-error</p><p>      tal:repeat</p><p>      tal:replace</p><h2>  attributes: Replace element attributes</h2>
<h3>    Syntax</h3>
<p>      <code>tal:attributes</code> syntax:<pre>        argument             ::= attribute_statement [';' attribute_statement]*
        attribute_statement  ::= attribute_name expression
        attribute_name       ::= [namespace ':'] Name
        namespace            ::= Name</pre>
</p><p>      <em>Note: If you want to include a semi-colon (;) in an <code>expression</code>,
      it must be escaped by doubling it (;;).</em></p><h3>    Description</h3>
<p>      The <code>tal:attributes</code> statement replaces the value of an attribute
      (or creates an attribute) with a dynamic value.  You can qualify an
      attribute name with a namespace prefix, for example <code>html:table</code>, if
      you are generating an XML document with multiple namespaces.  The
      value of each expression is converted to a string, if necessary.</p><p>      If the expression associated with an attribute assignment evaluates
      to <em>nothing</em>, then that attribute is deleted from the statement
      element.  If the expression evaluates to <em>default</em>, then that
      attribute is left unchanged.  Each attribute assignment is
      independent, so attributes may be assigned in the same statement in
      which some attributes are deleted and others are left alone.</p><p>      If you use <code>tal:attributes</code> on an element with an active
      <code>tal:replace</code> command, the <code>tal:attributes</code> statement is ignored.</p><p>      If you use <code>tal:attributes</code> on an element with a <code>tal:repeat</code>
      statement, the replacement is made on each repetition of the
      element, and the replacement expression is evaluated fresh for each
      repetition.</p><h3>    Examples</h3>
<p>      Replacing a link:<pre>        &lt;a href=&quot;/sample/link.html&quot;
         tal:attributes=&quot;href here/sub/absolute_url&quot;&gt;</pre>
</p><p>      Replacing two attributes:<pre>        &lt;textarea rows=&quot;80&quot; cols=&quot;20&quot;
         tal:attributes=&quot;rows request/rows;cols request/cols&quot;&gt;</pre>
</p><h2>  condition: Conditionally insert or remove an element</h2>
<h3>    Syntax</h3>
<p>      <code>tal:condition</code> syntax:<pre>        argument ::= expression</pre>
</p><h3>    Description</h3>
<p>      The <code>tal:condition</code> statement includes the statement element in the
      template only if the condition is met, and omits it otherwise.  If
      its expression evaluates to a <em>true</em> value, then normal processing
      of the element continues, otherwise the statement element is
      immediately removed from the template.  For these purposes, the
      value <em>nothing</em> is false, and <em>default</em> has the same effect as
      returning a true value.</p><p>      <em>Note: Zope considers missing variables, None, zero, empty strings,
      and empty sequences false; all other values are true.</em></p><h3>    Examples</h3>
<p>      Test a variable before inserting it (the first example tests for
      existence and truth, while the second only tests for existence):<pre>        &lt;p tal:condition=&quot;request/message | nothing&quot;
         tal:content=&quot;request/message&quot;&gt;message goes here&lt;/p&gt;

        &lt;p tal:condition=&quot;exists:request/message&quot;
         tal:content=&quot;request/message&quot;&gt;message goes here&lt;/p&gt;</pre>
</p><p>      Test for alternate conditions:<pre>        &lt;div tal:repeat=&quot;item python:range(10)&quot;&gt;
        &lt;p tal:condition=&quot;repeat/item/even&quot;&gt;Even&lt;/p&gt;
        &lt;p tal:condition=&quot;repeat/item/odd&quot;&gt;Odd&lt;/p&gt;
        &lt;/div&gt;</pre>
</p><h2>  content: Replace the content of an element</h2>
<h3>    Syntax</h3>
<p>      <code>tal:content</code> syntax:<pre>        argument ::= (['text'] | 'structure') expression</pre>
</p><h3>    Description</h3>
<p>      Rather than replacing an entire element, you can insert text or
      structure in place of its children with the <code>tal:content</code>
      statement.  The statement argument is exactly like that of
      <code>tal:replace</code>, and is interpreted in the same fashion.  If the
      expression evaluates to <em>nothing</em>, the statement element is left
      childless.  If the expression evaluates to <em>default</em>, then the
      element's contents are unchanged.</p><p>      <em>Note: The default replacement behavior is <code>text</code>.</em></p><h3>    Examples</h3>
<p>      Inserting the user name:<pre>        &lt;p tal:content=&quot;user/getUserName&quot;&gt;Fred Farkas&lt;/p&gt;</pre>
</p><p>      Inserting HTML/XML:<pre>        &lt;p tal:content=&quot;structure here/getStory&quot;&gt;marked &lt;b&gt;up&lt;/b&gt;
        content goes here.&lt;/p&gt;</pre>
</p><h3>    See Also</h3>
<p>      <code>tal:replace</code></p><h2>  define: Define variables</h2>
<h3>    Syntax</h3>
<p>      <code>tal:define</code> syntax:<pre>        argument       ::= define_scope [';' define_scope]*
        define_scope   ::= (['local'] | 'global') define_var
        define_var     ::= variable_name expression
        variable_name  ::= Name</pre>
</p><p>      <em>Note: If you want to include a semi-colon (;) in an <code>expression</code>,
      it must be escaped by doubling it (;;).</em></p><h3>    Description</h3>
<p>      The <code>tal:define</code> statement defines variables.  You can define two
      different kinds of TAL variables: local and global.  When you
      define a local variable in a statement element, you can only use
      that variable in that element and the elements it contains.  If
      you redefine a local variable in a contained element, the new
      definition hides the outer element's definition within the inner
      element.  When you define a global variables, you can use it in
      any element processed after the defining element.  If you redefine
      a global variable, you replace its definition for the rest of the
      template.</p><p>      <em>Note: local variables are the default</em></p><p>      If the expression associated with a variable evaluates to
      <em>nothing</em>, then that variable has the value <em>nothing</em>, and may be
      used as such in further expressions. Likewise, if the expression
      evaluates to <em>default</em>, then the variable has the value <em>default</em>,
      and may be used as such in further expressions.</p><h3>    Examples</h3>
<p>      Defining a global variable:<pre>        tal:define=&quot;global company_name string:Zope Corp, Inc.&quot;</pre>
</p><p>      Defining two variables, where the second depends on the first:<pre>        tal:define=&quot;mytitle template/title; tlen python:len(mytitle)&quot;</pre>
</p><h2>  omit-tag: Remove an element, leaving its contents</h2>
<h3>    Syntax</h3>
<p>      <code>tal:omit-tag</code> syntax:<pre>        argument ::= [ expression ]</pre>
</p><h3>    Description</h3>
<p>      The <code>tal:omit-tag</code> statement leaves the contents of a tag in place
      while omitting the surrounding start and end tag.</p><p>      If its expression evaluates to a <em>false</em> value, then normal
      processing of the element continues and the tag is not omitted.
      If the expression evaluates to a <em>true</em> value, or there is no
      expression, the statement tag is replaced with its contents. </p><p>      Zope treats empty strings, empty sequences, zero, None, <em>nothing</em>,
      and <em>default</em> at false. All other values are considered true.</p><h3>    Examples</h3>
<p>      Unconditionally omitting a tag:<pre>        &lt;div tal:omit-tag=&quot;&quot; comment=&quot;This tag will be removed&quot;&gt;
          &lt;i&gt;...but this text will remain.&lt;/i&gt;
        &lt;/div&gt;</pre>
</p><p>      Conditionally omitting a tag:<pre>        &lt;b tal:omit-tag=&quot;not:bold&quot;&gt;I may be bold.&lt;/b&gt;</pre>
</p><p>      The above example will omit the <code>b</code> tag if the variable <code>bold</code> is
      false.</p><p>      Creating ten paragraph tags, with no enclosing tag:<pre>        &lt;span tal:repeat=&quot;n python:range(10)&quot;
              tal:omit-tag=&quot;&quot;&gt;
          &lt;p tal:content=&quot;n&quot;&gt;1&lt;/p&gt;
        &lt;/span&gt;</pre>
</p><h2>  on-error: Handle errors</h2>
<h3>    Syntax</h3>
<p>      <code>tal:on-error</code> syntax:<pre>        argument ::= (['text'] | 'structure') expression</pre>
</p><h3>    Description</h3>
<p>      The <code>tal:on-error</code> statement provides error handling for your
      template.  When a TAL statement produces an error, the TAL
      interpreter searches for a <code>tal:on-error</code> statement on the same
      element, then on the enclosing element, and so forth. The first
      <code>tal:on-error</code> found is invoked. It is treated as a <code>tal:content</code>
      statement.</p><p>      A local variable <code>error</code> is set. This variable has these
      attributes:<dl>
<dt>        <code>type</code></dt>
<dd>the exception type</dd>
<dt>        <code>value</code></dt>
<dd>the exception instance</dd>
<dt>        <code>traceback</code></dt>
<dd>the traceback object</dd>
</dl>
</p><p>      The simplest sort of <code>tal:on-error</code> statement has a literal error
      string or <em>nothing</em> for an expression.  A more complex handler may
      call a script that examines the error and either emits error text
      or raises an exception to propagate the error outwards.  </p><h3>    Examples</h3>
<p>      Simple error message:<pre>        &lt;b tal:on-error=&quot;string: Username is not defined!&quot; 
         tal:content=&quot;here/getUsername&quot;&gt;Ishmael&lt;/b&gt;</pre>
</p><p>      Removing elements with errors:<pre>        &lt;b tal:on-error=&quot;nothing&quot;
           tal:content=&quot;here/getUsername&quot;&gt;Ishmael&lt;/b&gt;</pre>
</p><p>      Calling an error-handling script:<pre>        &lt;div tal:on-error=&quot;structure here/errorScript&quot;&gt;
          ...
        &lt;/div&gt;</pre>
</p><p>      Here's what the error-handling script might look like:<pre>        ## Script (Python) &quot;errHandler&quot;
        ##bind namespace=_
        ##
        error=_['error']
        if error.type==ZeroDivisionError:
            return &quot;&lt;p&gt;Can't divide by zero.&lt;/p&gt;&quot;
        else
            return &quot;&quot;&quot;&lt;p&gt;An error ocurred.&lt;/p&gt;
                      &lt;p&gt;Error type: %s&lt;/p&gt;
                      &lt;p&gt;Error value: %s&lt;/p&gt;&quot;&quot;&quot; % (error.type,
                                                   error.value)</pre>
</p><h3>    See Also</h3>
<p>      <a href="http://www.python.org/doc/current/tut/node10.html">Python Tutorial: Errors and
      Exceptions</a></p><p>      <a href="http://www.python.org/doc/current/lib/module-exceptions.html">Python Built-in
      Exceptions</a></p><h2>  repeat: Repeat an element</h2>
<h3>    Syntax</h3>
<p>      <code>tal:repeat</code> syntax:<pre>        argument      ::= variable_name expression
        variable_name ::= Name</pre>
</p><h3>    Description</h3>
<p>      The <code>tal:repeat</code> statement replicates a sub-tree of your document
      once for each item in a sequence. The expression should evaluate
      to a sequence. If the sequence is empty, then the statement
      element is deleted, otherwise it is repeated for each value in the
      sequence.  If the expression is <em>default</em>, then the element is
      left unchanged, and no new variables are defined.</p><p>      The <code>variable_name</code> is used to define a local variable and a
      repeat variable. For each repetition, the local variable is set to
      the current sequence element, and the repeat variable is set to an
      iteration object.</p><h3>    Repeat Variables</h3>
<p>      You use repeat variables to access information about the current
      repetition (such as the repeat index).  The repeat variable has
      the same name as the local variable, but is only accessible
      through the built-in variable named <code>repeat</code>.</p><h4>      The following information is available from the repeat variable:</h4>
<ul>
<li><em>index</em> - repetition number, starting from zero.</li>
<li><em>number</em> - repetition number, starting from one.</li>
<li><em>even</em> - true for even-indexed repetitions (0, 2, 4, ...).</li>
<li><em>odd</em> - true for odd-indexed repetitions (1, 3, 5, ...).</li>
<li><em>start</em> - true for the starting repetition (index 0).</li>
<li><em>end</em> - true for the ending, or final, repetition.</li>
<li><em>first</em> - true for the first item in a group - see note below</li>
<li><em>last</em> - true for the last item in a group - see note below</li>
<li><em>length</em> - length of the sequence, which will be the total number
          of repetitions.</li>
<li><em>letter</em> - repetition number as a lower-case letter: "a" -
          "z", "aa" - "az", "ba" - "bz", ..., "za" - "zz", "aaa" - "aaz",
          and so forth.</li>
<li><em>Letter</em> - upper-case version of <em>letter</em>.</li>
<li><em>roman</em> - repetition number as a lower-case roman numeral:
          "i", "ii", "iii", "iv", "v", etc.</li>
<li><em>Roman</em> - upper-case version of <em>roman</em>.</li>
</ul>
<p>      You can access the contents of the repeat variable using path
      expressions or Python expressions.  In path expressions, you write
      a three-part path consisting of the name <code>repeat</code>, the statement
      variable's name, and the name of the information you want, for
      example, <code>repeat/item/start</code>.  In Python expressions, you use
      normal dictionary notation to get the repeat variable, then
      attribute access to get the information, for example,
      "python:repeat['item'].start".</p><p>      Note that <code>first</code> and <code>last</code> are intended for use with sorted
      sequences.  They try to divide the sequence into group of items
      with the same value.  If you provide a path, then the value
      obtained by following that path from a sequence item is used for
      grouping, otherwise the value of the item is used.  You can
      provide the path by passing it as a parameter, as in
      "python:repeat['item'].first('color')", or by appending it to the
      path from the repeat variable, as in "repeat/item/first/color".</p><h3>    Examples</h3>
<h4>      Iterating over a sequence of strings::    </h4>
<p>        <p tal:repeat="txt python:'one', <code>two</code>, 'three'">
           <span tal:replace="txt" />
        </p></p><p>      Inserting a sequence of table rows, and using the repeat variable
      to number the rows:<pre>        &lt;table&gt;
          &lt;tr tal:repeat=&quot;item here/cart&quot;&gt;
              &lt;td tal:content=&quot;repeat/item/number&quot;&gt;1&lt;/td&gt;
              &lt;td tal:content=&quot;item/description&quot;&gt;Widget&lt;/td&gt;
              &lt;td tal:content=&quot;item/price&quot;&gt;$1.50&lt;/td&gt;
          &lt;/tr&gt;
        &lt;/table&gt;</pre>
</p><p>      Nested repeats:<pre>        &lt;table border=&quot;1&quot;&gt;
          &lt;tr tal:repeat=&quot;row python:range(10)&quot;&gt;
            &lt;td tal:repeat=&quot;column python:range(10)&quot;&gt;
              &lt;span tal:define=&quot;x repeat/row/number; 
                                y repeat/column/number; 
                                z python:x*y&quot;
                    tal:replace=&quot;string:$x * $y = $z&quot;&gt;1 * 1 = 1&lt;/span&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
        &lt;/table&gt;</pre>
</p><p>      Insert objects. Seperates groups of objects by meta-type by
      drawing a rule between them:<pre>        &lt;div tal:repeat=&quot;object objects&quot;&gt;
          &lt;h2 tal:condition=&quot;repeat/object/first/meta_type&quot;
            tal:content=&quot;object/meta_type&quot;&gt;Meta Type&lt;/h2&gt;
          &lt;p tal:content=&quot;object/getId&quot;&gt;Object ID&lt;/p&gt;
          &lt;hr tal:condition=&quot;repeat/object/last/meta_type&quot; /&gt;
        &lt;/div&gt;</pre>
</p><p>      Note, the objects in the above example should already be sorted by
      meta-type.</p><h2>  replace: Replace an element</h2>
<h3>    Syntax</h3>
<p>      <code>tal:replace</code> syntax:<pre>        argument ::= (['text'] | 'structure') expression</pre>
</p><h3>    Description  </h3>
<p>      The <code>tal:replace</code> statement replaces an element with dynamic
      content.  It replaces the statement element with either text or a
      structure (unescaped markup).  The body of the statement is an
      expression with an optional type prefix.  The value of the
      expression is converted into an escaped string if you prefix the
      expression with <code>text</code> or omit the prefix, and is inserted
      unchanged if you prefix it with <code>structure</code>.  Escaping consists of
      converting "&amp;" to "&amp;amp;", "&lt;" to "&amp;lt;", and
      "&gt;" to "&amp;gt;".</p><p>      If the value is <em>nothing</em>, then the element is simply removed.  If
      the value is <em>default</em>, then the element is left unchanged.</p><h3>    Examples</h3>
<p>      The two ways to insert the title of a template:<pre>        &lt;span tal:replace=&quot;template/title&quot;&gt;Title&lt;/span&gt;
        &lt;span tal:replace=&quot;text template/title&quot;&gt;Title&lt;/span&gt;</pre>
</p><p>      Inserting HTML/XML:<pre>        &lt;div tal:replace=&quot;structure table&quot; /&gt;</pre>
</p><p>      Inserting nothing:<pre>        &lt;div tal:replace=&quot;nothing&quot;&gt;This element is a comment.&lt;/div&gt;</pre>
</p><h3>    See Also</h3>
<p>      <code>tal:content</code></p><h2>  TALES Overview</h2>
<p>    The <em>Template Attribute Language Expression Syntax</em> (TALES) standard
    describes expressions that supply TAL and
    METAL with data.  TALES is <em>one</em> possible expression
    syntax for these languages, but they are not bound to this
    definition.  Similarly, TALES could be used in a context having
    nothing to do with TAL or METAL.</p><p>    TALES expressions are described below with any delimiter or quote
    markup from higher language layers removed.  Here is the basic
    definition of TALES syntax:<pre>      Expression  ::= [type_prefix ':'] String
      type_prefix ::= Name</pre>
</p><p>    Here are some simple examples:<pre>      a/b/c
      path:a/b/c
      nothing
      path:nothing
      python: 1 + 2
      string:Hello, ${user/getUserName}</pre>
</p><p>    The optional <em>type prefix</em> determines the semantics and syntax of
    the <em>expression string</em> that follows it.  A given implementation of
    TALES can define any number of expression types, with whatever
    syntax you like. It also determines which expression type is
    indicated by omitting the prefix.</p><p>    If you do not specify a prefix, Zope assumes that the expression is
    a <em>path</em> expression.</p><h3>    TALES Expression Types</h3>
<p>      These are the TALES expression types supported by Zope:</p><ul>
<li>path expressions - locate a value by its path.</li>
<li>exists expressions - test whether a path is valid.</li>
<li>nocall expressions - locate an object by its path.</li>
<li>not expressions - negate an expression</li>
<li>string expressions - format a string</li>
<li>python expressions - execute a Python
        expression</li>
</ul>
<h3>    Built-in Names</h3>
<p>      These are the names that always available to TALES expressions in Zope:</p><ul>
<li><em>nothing</em> - special value used by to represent 
         a <em>non-value</em> (e.g. void, None, Nil, NULL).</li>
<li><em>default</em> - special value used to specify that
        existing text should not be replaced. See the documentation for
        individual TAL statements for details on how they interpret
        <em>default</em>.</li>
<li><em>options</em> - the <em>keyword</em> arguments passed to the template. These
        are generally available when a template is called from Methods
        and Scripts, rather than from the web.</li>
<li><em>repeat</em> - the <code>repeat</code> variables; see the
        tal:repeat documentation.</li>
<li><em>attrs</em> - a dictionary containing the initial values of the
        attributes of the current statement tag.</li>
<li><em>CONTEXTS</em> - the list of standard names (this list).  This can be
        used to access a built-in variable that has been hidden by a local
        or global variable with the same name.</li>
<li><em>root</em> - the system's top-most object: the Zope root folder.</li>
<li><em>here</em> - the object to which the template is being applied.</li>
<li><em>container</em> - The folder in which the template is located.</li>
<li><em>template</em> - the template itself.</li>
<li><em>request</em> - the publishing request object.</li>
<li><em>user</em> - the authenticated user object.</li>
<li><em>modules</em> - a collection through which Python modules and
        packages can be accessed.  Only modules which are approved by
        the Zope security policy can be accessed.</li>
</ul>
<p>      Note the names <code>root</code>, <code>here</code>, <code>container</code>, <code>template</code>, <code>request</code>,
      <code>user</code>, and <code>modules</code> are optional names supported by Zope, but
      are not required by the TALES standard.</p><h3>    See Also</h3>
<p>      TAL Overview</p><p>      METAL Overview</p><p>      exists expressions</p><p>      nocall expressions</p><p>      not expressions</p><p>      string expressions</p><p>      path expressions</p><p>      python expressions</p><h2>  TALES Exists expressions</h2>
<h3>    Syntax</h3>
<p>      Exists expression syntax:<pre>        exists_expressions ::= 'exists:' path_expression</pre>
</p><h3>    Description</h3>
<p>      Exists expressions test for the existence of paths.  An exists
      expression returns true when the path expressions following it
      expression returns a value. It is false when the path expression
      cannot locate an object.</p><h3>    Examples</h3>
<p>      Testing for the existence of a form variable:<pre>        &lt;p tal:condition=&quot;not:exists:request/form/number&quot;&gt;
          Please enter a number between 0 and 5
        &lt;/p&gt;</pre>
</p><p>      Note that in this case you can't use the expression,
      <code>not:request/form/number</code>, since that expression will be true if
      the <code>number</code> variable exists and is zero.</p><h2>  TALES Nocall expressions</h2>
<h3>    Syntax</h3>
<p>      Nocall expression syntax:<pre>        nocall_expression ::= 'nocall:' path_expression</pre>
</p><h3>    Description</h3>
<p>      Nocall expressions avoid rendering the results of a path
      expression.</p><p>      An ordinary path expression tries to render the object that it
      fetches.  This means that if the object is a function, Script,
      Method, or some other kind of executable thing, then expression
      will evaluate to the result of calling the object.  This is
      usually what you want, but not always.  For example, if you want
      to put a DTML Document into a variable so that you can refer to
      its properties, you can't use a normal path expression because it
      will render the Document into a string.</p><h3>    Examples</h3>
<p>      Using nocall to get the properties of a document:<pre>        &lt;span tal:define=&quot;doc nocall:here/aDoc&quot;
              tal:content=&quot;string:${doc/getId}: ${doc/title}&quot;&gt;
        Id: Title&lt;/span&gt;</pre>
</p><p>      Using nocall expressions on a functions:<pre>        &lt;p tal:define=&quot;join nocall:modules/string/join&quot;&gt;</pre>
</p><p>      This example defines a variable <code>join</code> which is bound to the
      <code>string.join</code> function.</p><h2>  TALES Not expressions</h2>
<h3>    Syntax</h3>
<p>      Not expression syntax:<pre>        not_expression ::= 'not:' expression</pre>
</p><h3>    Description</h3>
<p>      Not expression evaluate the expression string (recursively) as a
      full expression, and returns the boolean negation of its value. If
      the expression supplied does not evaluate to a boolean value,
      <em>not</em> will issue a warning and <em>coerce</em> the expression's value
      into a boolean type based on the following rules:</p><ol>
<li> the number 0 is <em>false</em></li>
<li> numbers > 0 are <em>true</em></li>
<li> an empty string or other sequence is <em>false</em></li>
<li> a non-empty string or other sequence is <em>true</em></li>
<li> a <em>non-value</em> (e.g. void, None, Nil, NULL, etc) is <em>false</em></li>
<li> all other values are implementation-dependent.</li>
</ol>
<p>      If no expression string is supplied, an error should be generated.</p><p>      Zope considers all objects not specifically listed above as
      <em>false</em> (including negative numbers) to be <em>true</em>.</p><h3>    Examples</h3>
<p>      Testing a sequence:<pre>        &lt;p tal:condition=&quot;not:here/objectIds&quot;&gt;
          There are no contained objects.
        &lt;/p&gt;</pre>
</p><h2>  TALES Path expressions</h2>
<h3>    Syntax</h3>
<p>      Path expression syntax:<pre>        PathExpr  ::= Path [ '|' Path ]*
        Path      ::= variable [ '/' URL_Segment ]*
        variable  ::= Name</pre>
</p><h3>    Description</h3>
<p>      A path expression consists of one or more <em>paths</em> separated by
      vertical bars (|).  A path consists of one or more non-empty
      strings separated by slashes. The first string must be a variable
      name (built-in variable or a user defined variable), and the
      remaining strings, the <em>path segments</em>, may contain letters,
      digits, spaces, and the punctuation characters underscore, dash,
      period, comma, and tilde.</p><p>      For example:<pre>        request/cookies/oatmeal
        nothing
        here/some-file 2001_02.html.tar.gz/foo
        root/to/branch | default
        request/name | string:Anonymous Coward</pre>
</p><p>      When a path expression is evaluated, Zope attempts to traverse the
      path, from left to right, until it succeeds or runs out of paths
      segments.  To traverse a path, it first fetches the object stored
      in the variable.  For each path segment, it traverses from the
      current object to the subobject named by the path
      segment. Subobjects are located according to standard Zope
      traversal rules (via getattr, getitem, or traversal hooks).</p><p>      Once a path has been successfully traversed, the resulting object
      is the value of the expression.  If it is a callable object, such
      as a method or template, it is called.</p><p>      If a traversal step fails, evaluation immediately proceeds to the next
      path.  If there are no further paths, an error results.</p><p>      The expression in a series of paths seperated by vertical bars can
      be any TALES expression. For example, 'request/name |
      string:Anonymous Coward'. This is useful chiefly for providing
      default values such as strings and numbers which are not
      expressable as path expressions.</p><p>      If no path is given the result is <em>nothing</em>.</p><p>      Since every path must start with a variable name, you need a set
      of starting variables that you can use to find other objects and
      values.  See the TALES overview for a list of
      built-in variables.  Since variable names are looked up first in
      locals, then in globals, then in this list, these names act just
      like built-ins in Python; They are always available, but they can
      be shadowed by a global or local variable declaration. You can
      always access the built-in names explicitly by prefixing them with
      <em>CONTEXTS</em>. (e.g. CONTEXTS/root, CONTEXTS/nothing, etc).</p><h3>    Examples</h3>
<p>      Inserting a cookie variable or a property:<pre>        &lt;span tal:replace=&quot;request/cookies/pref | here/pref&quot;&gt;
          preference
        &lt;/span&gt;</pre>
</p><p>      Inserting the user name:<pre>        &lt;p tal:content=&quot;user/getUserName&quot;&gt;
          User name
        &lt;/p&gt;</pre>
</p><h2>  TALES Python expressions</h2>
<h3>    Syntax</h3>
<p>      Python expression syntax:<pre>        Any valid Python language expression</pre>
</p><h3>    Description</h3>
<p>      Python expressions evaluate Python code in a security-restricted
      environment. Python expressions offer the same facilities as those
      available in Python-based Scripts and DTML variable expressions.</p><h4>      Security Restrictions</h4>
<p>        Python expressions are subject to the same security restrictions
        as Python-based scripts. These restrictions include:</p><dl>
<dt>        access limits</dt>
<dd>Python expressions are subject to Zope
        permission and role security restrictions. In addition,
        expressions cannot access objects whose names begin with
        underscore.</dd>
<dt>        write limits</dt>
<dd>Python expressions cannot change attributes of
        Zope objects.</dd>
</dl>
<p>        Despite these limits malicious Python expressions can cause
        problems.  See The Zope Book for more information.</p><h4>      Built-in Functions</h4>
<p>        Python expressions have the same built-ins as Python-based
        Scripts with a few additions.</p><p>        These standard Python built-ins are available: <code>None</code>, <code>abs</code>,
        <code>apply</code>, <code>callable</code>, <code>chr</code>, <code>cmp</code>, <code>complex</code>, <code>delattr</code>,
        <code>divmod</code>, <code>filter</code>, <code>float</code>, <code>getattr</code>, <code>hash</code>, <code>hex</code>, <code>int</code>,
        <code>isinstance</code>, <code>issubclass</code>, <code>list</code>, <code>len</code>, <code>long</code>, <code>map</code>, <code>max</code>,
        <code>min</code>, <code>oct</code>, <code>ord</code>, <code>repr</code>, <code>round</code>, <code>setattr</code>, <code>str</code>, <code>tuple</code>.</p><p>        The <code>range</code> and <code>pow</code> functions are available and work the same
        way they do in standard Python; however, they are limited to
        keep them from generating very large numbers and sequences. This
        limitation helps protect against denial of service attacks.</p><p>        In addition, these utility functions are available: <code>DateTime</code>,
        <code>test</code>, and <code>same_type</code>. See DTML
        functions for more
        information on these functions.</p><p>        Finally, these functions are available in Python expressions,
        but not in Python-based scripts:</p><dl>
<dt>        <code>path(string)</code></dt>
<dd>Evaluate a TALES path
        expression.</dd>
<dt>        <code>string(string)</code></dt>
<dd>Evaluate a TALES string
        expression.</dd>
<dt>        <code>exists(string)</code></dt>
<dd>Evaluates a TALES exists
        expression.</dd>
<dt>        <code>nocall(string)</code></dt>
<dd>Evaluates a TALES nocall
        expression.</dd>
</dl>
<h4>      Python Modules</h4>
<p>        A number of Python modules are available by default. You can
        make more modules available. You can access modules either via
        path expressions (for example 'modules/string/join') or in
        Python with the <code>modules</code> mapping object (for example
        'modules["string"].join'). Here are the default modules:</p><dl>
<dt>        <code>string</code></dt>
<dd>The standard <a href="http://www.python.org/doc/current/lib/module-string.html">Python string
        module</a>. Note:
        most of the functions in the module are also available as
        methods on string objects.</dd>
<dt>        <code>random</code></dt>
<dd>The standard <a href="http://www.python.org/doc/current/lib/module-random.html">Python random
        module</a>.</dd>
<dt>        <code>math</code></dt>
<dd>The standard <a href="http://www.python.org/doc/current/lib/module-math.html">Python math
        module</a>.</dd>
<dt>        <code>sequence</code></dt>
<dd>A module with a powerful sorting function. See
        sequence for more information.</dd>
<dt>        <code>Products.PythonScripts.standard</code></dt>
<dd>Various HTML formatting
        functions available in DTML. See
        Products.PythonScripts.standard
        for more information.</dd>
<dt>        <code>ZTUtils</code></dt>
<dd>Batch processing facilities similar to those
        offered by <code>dtml-in</code>. See ZTUtils
        for more information.</dd>
<dt>        <code>AccessControl</code></dt>
<dd>Security and access checking facilities. See
        AccessControl for more
        information.</dd>
</dl>
<h3>    Examples</h3>
<p>      Using a module usage (pick a random choice from a list):<pre>        &lt;span tal:replace=&quot;python:modules['random'].choice(['one', 
                             'two', 'three', 'four', 'five'])&quot;&gt;
          a random number between one and five
        &lt;/span&gt;</pre>
</p><p>      String processing (capitalize the user name):<pre>        &lt;p tal:content=&quot;python:user.getUserName().capitalize()&quot;&gt;
          User Name
        &lt;/p&gt;</pre>
</p><p>      Basic math (convert an image size to megabytes):<pre>        &lt;p tal:content=&quot;python:image.getSize() / 1048576.0&quot;&gt;
          12.2323
        &lt;/p&gt;</pre>
</p><p>      String formatting (format a float to two decimal places):<pre>        &lt;p tal:content=&quot;python:'%0.2f' % size&quot;&gt;
          13.56
        &lt;/p&gt;</pre>
</p><h2>  TALES String expressions</h2>
<h3>    Syntax</h3>
<p>      String expression syntax:<pre>        string_expression ::= ( plain_string | [ varsub ] )*
        varsub            ::= ( '$' Path ) | ( '${' Path '}' )
        plain_string      ::= ( '$$' | non_dollar )*
        non_dollar        ::= any character except '$'</pre>
</p><h3>    Description</h3>
<p>      String expressions interpret the expression string as text. If no
      expression string is supplied the resulting string is <em>empty</em>. The
      string can contain variable substitutions of the form <code>$name</code> or
      <code>${path}</code>, where <code>name</code> is a variable name, and <code>path</code> is a
      path expression.
      The escaped string value of the path expression is inserted into
      the string. To prevent a <code>$</code> from being interpreted this way, it
      must be escaped as <code>$$</code>.</p><h3>    Examples</h3>
<p>      Basic string formatting:<pre>        &lt;span tal:replace=&quot;string:$this and $that&quot;&gt;
          Spam and Eggs
        &lt;/span&gt;</pre>
</p><p>      Using paths:<pre>        &lt;p tal:content=&quot;total: ${request/form/total}&quot;&gt;
          total: 12
        &lt;/p&gt;</pre>
</p><p>      Including a dollar sign:<pre>        &lt;p tal:content=&quot;cost: $$$cost&quot;&gt;
          cost: $42.00
        &lt;/p&gt;</pre>
</p><h2>  METAL Overview</h2>
<p>    The <em>Macro Expansion Template Attribute Language</em> (METAL) standard
    is a facility for HTML/XML macro preprocessing. It can be used in
    conjunction with or independently of TAL and
    TALES.</p><p>    Macros provide a way to define a chunk of presentation in one
    template, and share it in others, so that changes to the macro are
    immediately reflected in all of the places that share it.
    Additionally, macros are always fully expanded, even in a template's
    source text, so that the template appears very similar to its final
    rendering.</p><h3>    METAL Namespace</h3>
<p>      The METAL namespace URI and recommended alias are currently
      defined as:<pre>        xmlns:metal=&quot;http://xml.zope.org/namespaces/metal&quot;</pre>
</p><p>      Just like the TAL namespace URI, this URI is not attached to a web
      page; it's just a unique identifier.</p><p>      Zope does not require an XML namespace declaration when creating
      templates with a content-type of <code>text/html</code>. However, it does
      require an XML namespace declaration for all other content-types.</p><h3>    METAL Statements </h3>
<p>      METAL defines a number of statements:</p><ul>
<li>metal:define-macro - Define a macro.</li>
<li>metal:use-macro - Use a macro.</li>
<li>metal:define-slot - Define a macro
      customization point.</li>
<li>metal:fill-slot - Customize a macro.</li>
</ul>
<p>      Although METAL does not define the syntax of expression
      non-terminals, leaving that up to the implementation, a canonical
      expression syntax for use in METAL arguments is described in
      TALES Specification.</p><h3>    See Also</h3>
<p>      TAL Overview</p><p>      TALES Overview</p><p>      metal:define-macro</p><p>      metal:use-macro</p><p>      metal:define-slot</p><p>      metal:fill-slot</p><h2>  define-macro: Define a macro</h2>
<h3>    Syntax</h3>
<p>      <code>metal:define-macro</code> syntax:<pre>        argument ::= Name</pre>
</p><h3>    Description</h3>
<p>      The <code>metal:define-macro</code> statement defines a macro. The macro is
      named by the statement expression, and is defined as the element
      and its sub-tree.</p><p>      In Zope, a macro definition is available as a sub-object of a
      template's <code>macros</code> object. For example, to access a macro named
      <code>header</code> in a template named <code>master.html</code>, you could use the path
      expression <code>master.html/macros/header</code>.</p><h3>    Examples</h3>
<p>      Simple macro definition:<pre>        &lt;p metal:define-macro=&quot;copyright&quot;&gt;
          Copyright 2001, &lt;em&gt;Foobar&lt;/em&gt; Inc.
        &lt;/p&gt;</pre>
</p><h3>    See Also</h3>
<p>      metal:use-macro</p><p>      metal:define-slot</p><h2>  define-slot: Define a macro customization point</h2>
<h3>    Syntax</h3>
<p>      <code>metal:define-slot</code> syntax:<pre>        argument ::= Name</pre>
</p><h3>    Description</h3>
<p>      The <code>metal:define-slot</code> statement defines a macro customization
      point or <em>slot</em>. When a macro is used, its slots can be replaced,
      in order to customize the macro. Slot definitions provide default
      content for the slot. You will get the default slot contents if
      you decide not to customize the macro when using it.</p><p>      The <code>metal:define-slot</code> statement must be used inside a
      <code>metal:define-macro</code> statement.</p><p>      Slot names must be unique within a macro.</p><h3>    Examples</h3>
<p>      Simple macro with slot:<pre>        &lt;p metal:define-macro=&quot;hello&quot;&gt;
          Hello &lt;b metal:define-slot=&quot;name&quot;&gt;World&lt;/b&gt;
        &lt;/p&gt;</pre>
</p><p>      This example defines a macro with one slot named <code>name</code>. When you
      use this macro you can customize the <code>b</code> element by filling the
      <code>name</code> slot.</p><h3>    See Also</h3>
<p>      metal:fill-slot</p><h2>  fill-slot: Customize a macro</h2>
<h3>    Syntax</h3>
<p>      <code>metal:fill-slot</code> syntax:<pre>        argument ::= Name</pre>
</p><h3>    Description</h3>
<p>      The <code>metal:fill-slot</code> statement customizes a macro by replacing a
      <em>slot</em> in the macro with the statement element (and its content).</p><p>      The <code>metal:fill-slot</code> statement must be used inside a
      <code>metal:use-macro</code> statement.</p><p>      Slot names must be unique within a macro.</p><p>      If the named slot does not exist within the macro, the slot
      contents will be silently dropped.</p><h3>    Examples</h3>
<p>     Given this macro:<pre>        &lt;p metal:define-macro=&quot;hello&quot;&gt;
          Hello &lt;b metal:define-slot=&quot;name&quot;&gt;World&lt;/b&gt;
        &lt;/p&gt;</pre>
</p><p>      You can fill the <code>name</code> slot like so:<pre>        &lt;p metal:use-macro=&quot;container/master.html/macros/hello&quot;&gt;
          Hello &lt;b metal:fill-slot=&quot;name&quot;&gt;Kevin Bacon&lt;/b&gt;
        &lt;/p&gt;</pre>
</p><h3>    See Also</h3>
<p>      metal:define-slot</p><h2>  use-macro: Use a macro</h2>
<h3>    Syntax</h3>
<p>      <code>metal:use-macro</code> syntax:<pre>        argument ::= expression</pre>
</p><h3>    Description</h3>
<p>      The <code>metal:use-macro</code> statement replaces the statement element
      with a macro. The statement expression describes a macro
      definition. </p><p>      In Zope the expression will generally be a path expression
      referring to a macro defined in another template. See
      "metal:define-macro" for more information.</p><p>      The effect of expanding a macro is to graft a subtree from another
      document (or from elsewhere in the current document) in place of
      the statement element, replacing the existing sub-tree.  Parts of
      the original subtree may remain, grafted onto the new subtree, if
      the macro has <em>slots</em>. See
      metal:define-slot for more information. If
      the macro body uses any macros, they are expanded first.</p><p>      When a macro is expanded, its <code>metal:define-macro</code> attribute is
      replaced with the <code>metal:use-macro</code> attribute from the statement
      element.  This makes the root of the expanded macro a valid
      <code>use-macro</code> statement element.</p><h3>    Examples</h3>
<p>      Basic macro usage:<pre>        &lt;p metal:use-macro=&quot;container/other.html/macros/header&quot;&gt;
          header macro from defined in other.html template
        &lt;/p&gt;</pre>
</p><p>      This example refers to the <code>header</code> macro defined in the
      <code>other.html</code> template which is in the same folder as the current
      template. When the macro is expanded, the <code>p</code> element and its
      contents will be replaced by the macro. Note: there will still be
      a <code>metal:use-macro</code> attribute on the replacement element.</p><h3>    See Also</h3>
<p>      metal:define-macro</p><p>      metal:fill-slot</p></body>
</html>