Edgewall Software

Changes between Version 9 and Version 10 of MarkupTemplates


Ignore:
Timestamp:
Jul 9, 2006, 3:45:29 PM (18 years ago)
Author:
cmlenz
Comment:

More on context and expressions

Legend:

Unmodified
Added
Removed
Modified
  • MarkupTemplates

    v9 v10  
    77[[PageOutline(2-3, Table of Contents, inline)]]
    88
    9 == Expressions ==
     9== Template Context ==
    1010
    11 Python expressions can be used in text and attribute values. An expression is substituted with the result of its' evaluation against the template data. Expressions need to prefixed with a dollar sign (`$`) and usually enclosed by curly braces (`{…}`).
     11Rendering a template boils down to applying one or more template files to some input data. This input data is represented as a [wiki:ApiDocs/MarkupTemplate#markup.template:Context Context] object in Markup. A context is basically a stack of dictionaries that manages the namespace scopes during processing: for example, some variable may be able under a given name inside a loop, but not outside of that loop.
    1212
    13 == Directives ==
     13Regardless of its stack behavior during templating processing, a context is usually initialized from a single dictionary:
     14
     15{{{
     16>>> from markup.template import Context
     17>>> ctxt = Context(title='Hello, world!')
     18>>> print ctxt
     19[{'title': 'Hello, world!'}]
     20}}}
     21
     22This data is then made available for use inside the template:
     23
     24{{{
     25>>> from markup.template import Template
     26>>> tmpl = Template('<h1>$title</h1>')
     27>>> print tmpl.generate(ctxt)
     28<h1>Hello, world!</h1>
     29}}}
     30
     31Here the `title` variable in the context is accessed through a ''template expression'', discussed below.
     32
     33== Template Expressions ==
     34
     35Python expressions can be used in text and attribute values. An expression is substituted with the result of its evaluation against the template data. Expressions need to prefixed with a dollar sign (`$`) and usually enclosed in curly braces (`{…}`).
     36
     37If the expression starts with a letter and contains only letters and digits, the curly braces may be omitted. In all other cases, the braces are required so that the template processors knows where the expression ends:
     38
     39{{{
     40>>> from markup.template import Context, Template
     41>>> tmpl = Template('<em>${items[0].capitalize()} item</em>')
     42>>> print tmpl.generate(Context(items=['first', 'second']))
     43<em>First item</em>
     44}}}
     45
     46Expressions support the full power of Python. In addition, it is possible to:
     47 * access items in a dictionary using ''“dotted notation”'' (i.e. as if they were attributes)
     48 * access attributes using item access (i.e. as if they were items in a dictionary)
     49
     50{{{
     51>>> from markup.template import Context, Template
     52>>> tmpl = Template('<em>${dict.foo}</em>')
     53>>> print tmpl.generate(Context(dict={'foo': 'bar'}))
     54<em>bar</em>
     55}}}
     56
     57== Template Directives ==
    1458
    1559=== `py:content` ===
     
    2973=== `py:match` ===
    3074
    31 == Includes ==
     75== Template Includes ==