Directives reference ==================== Interpolations -------------- Interpolations are delimited by ``${...}`` (or just a leading ``$`` in the case of a simple python identifier), and may contain any python expression: .. code:: html

$body.content

${', '.join(names)}

HTML escaping can be turned off with $!, for example:: $!body.content $!{', '.join(names)} You can also mark values as Markup to avoid autoescaping:: ${Markup(body.content)} .. _py-block: py:block -------- ``py:block`` defines a block of HTML code that may be customized within a template or a template function. It requires a single ``name`` argument. Syntax: .. code:: html

Heading goes here

Content goes here When used as an attribute, the inner HTML of the element becomes the block. These two forms are equivalent: .. code:: html

Heading goes here

Heading goes here

Internally this is compiled to: .. code:: python def __root__(heading=Markup("Heading goes here")): yield "

" yield Markup(heading) yield "

" .. _py-call: py:call ------- Why use a ```` directive instead of just calling a function as an interpolation? Because it lets you pass chunks of template code as arguments! Whatever is inside the element is gathered together and compiled to a template function, which is then passed to the function as an argument. .. code:: html
${content()}
  • Fish and chips
  • Jelly and ice cream
  • Jelly and chips
You can also use the ``py:keyword`` directive to pass multiple arguments: .. code:: html
${content()}

Today's menu

  • Fish and chips
  • Jelly and ice cream
  • Jelly and chips
Because each template block passed as an argument is compiled to a template function, the function must call the argument to display the HTML. If you want to be able to pass either a string or a template block, use this idiom: .. code:: html ${content() if callable(content) else content} py:comment ---------- ``py:comment`` causes all content within the directive to be ignored and no output will be produced. In the following example, no output will be produced for either element: .. code:: html

The sun was shining on the sea

The sun was shining on the sea

py:def ------ Define a template function: .. code:: html The resulting function may be called within a regular python interpolation: .. code:: html
${modal("I'm sorry Dave, I'm afraid I can't do that", title="Error")}
Or with a ``py:call`` directive: .. code:: html .. _py-extends: py:extends ---------- ``py:extends`` replaces the contained element with the contents of the linked template file. ``py:block`` arguments can be used to customize code within. The attribute ``ignore-missing=""`` can be set to avoid raising an error if the extended template is not found. In this case, the ```` element will not be rendered (including any children) Example: .. code:: html Default title
Default content
.. code:: html Good morning Everybody! .. _py-include: py:include ---------- ``py:include`` may be used to include another template file. The attribute ``ignore-missing=""`` can be set to avoid raising an error if the included template is not found. Example: .. code:: html py:filter --------- ``py:filter`` passes the generated string content of the contained template code through a function: .. code:: html

This will be rendered in upper case

py:if ----- ``py:if`` defines a block of HTML code to be displayed conditionally. It requires a single ``test`` argument. Syntax: .. code:: html

Heading goes here

Content goes here When used as an attribute, the outer HTML of the element becomes the body of the if statement. These two forms are equivalent: .. code:: html

The sun was shining on the sea

The sun was shining on the sea

py:choose … py:when … py:otherwise ----------------------------------- ``py:choose`` defines a block similar to switch statement. The outer ``py:choose`` block may contain one or more ``py:when`` directives; only the first one whose ``test`` condition matches will be output. If no ``py:when`` block matches the contents of any ``py:otherwise`` will be output. Syntax: .. code:: html Message 1 Message 2 Fallthrough message

Message 1

Message 2

Fallthrough message

``py:choose`` may also take a ``test`` attribute: .. code:: html Message 1 Message 2 Fallthrough message

Message 1

Message 2

Fallthrough message

.. _py-strip: py:strip -------- ``py:strip`` causes the containing tag to be omitted from output. With no arguments, the containing tag is unconditionally stripped. If an expression argument is given the tag is only stripped if the expression evaluates to True: .. code:: html
.. _py-tag: py:tag ------ ``py:tag`` allows you to dynamically change the tag name of an element: .. code:: html … .. _py-whitespace: py:whitespace ------------------- Turns whitespace stripping on or off. A value of ``strip`` will cause all whitespace elements around opening and closing tags to be stripped. For example, this: .. code:: html
  • Hello World!
would render as this: .. code:: html
  • Hello World!
Within a ``py:whitespace="strip"`` block, ``py:whitespace="preserve"`` can be used to turn off stripping: .. code:: html
  • Hello World!
Would render as: .. code:: html
  • Hello World!
Whitespace is stripped according to these rules: - Whitespace is defined as the following characters: space (ascii 0x20), tab (0x09), line feed (0x0a) or carriage return (0x0d). Nonbreaking spaces or other unicode space characters are never stripped. - Whitespace at the start or end of an element is always stripped: `` foo `` → ``foo`` - Whitespace immediately before or after an element is stripped if it contains one or more newline or carriage return characters: So ``
  • item 1
  • \n
  • item 2
  • `` → ``
  • item 1
  • item 2
  • `` **but** ``click here`` → ``click here``. .. _py-whitespace: py:with ------------------- ``py:with`` allows you to assign variables. Variables are only in scope within the containing element. Use semicolons to separate multiple variable assignments. Examples: .. code:: html
    $result
    .. code:: html Published by $author on $pub_date Whitespace handling =================== Whitespace is maintained as per the input template file, with the following exceptions: - Whitespace between consecutive elements is always stripped, eg: .. code:: html $x, $y Would be output on a single line, ie ``0, 0, 0, 1, 1, 0, 1, 1``: - The :ref:`py-whitespace` directive causes all whitespace to be stripped from around tags inside the element. This includes spaces, tabs, carriage returns and newlines. Non-breaking spaces and other unicode spacing characters are not stripped