= Markup Templates = The most important feature provided by the Markup package is a template engine. Templates are XML files of some kind (such as XHTML) that include ''[wiki:MarkupTemplates#TemplateDirectives processing directives]'' (elements or attributes identified by a separate namespace) that affect how the template is rendered, and ''[wiki:MarkupTemplates#TemplateExpressions template expressions]'' that are dynamically substituted by variable data. This documentation is a work in progress and as of yet rather incomplete. ''Note that Markup templates are ''very'' similar to [http://kid-templating.org/ Kid] templates, so you may want to read the awesome [http://kid-templating.org/language.html language specification] for Kid for a more comprehensive guide. There exist some substantial differences between Kid and Markup, but the set of directives is basically the same (except for `py:extends` and `py:layout`, which don't exist in Markup, and `py:match`, which uses XPath in Markup).'' [[PageOutline(2-3, Table of Contents, inline)]] == Template Context == Rendering 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 available under a given name inside a loop, but not outside of that loop. Regardless of its stack behavior during templating processing, a context is usually initialized from a single dictionary: {{{ >>> from markup.template import Context >>> ctxt = Context(title='Hello, world!') >>> print ctxt [{'title': 'Hello, world!'}] }}} This data is then made available for use inside the template: {{{ >>> from markup.template import Template >>> tmpl = Template('

$title

') >>> print tmpl.generate(ctxt)

Hello, world!

}}} Here the `title` variable in the context is accessed through a ''template expression'', discussed below. == Template Expressions == 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 in curly braces (`{…}`). If 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: {{{ >>> from markup.template import Context, Template >>> tmpl = Template('${items[0].capitalize()} item') >>> print tmpl.generate(Context(items=['first', 'second'])) First item }}} Expressions support the full power of Python. In addition, it is possible to: * access items in a dictionary using ''“dotted notation”'' (i.e. as if they were attributes) * access attributes using item access (i.e. as if they were items in a dictionary) {{{ >>> from markup.template import Context, Template >>> tmpl = Template('${dict.foo}') >>> print tmpl.generate(Context(dict={'foo': 'bar'})) bar }}} == Template Directives == ''Directives'' are elements and/or attributes in the template that are identified by the namespace `http://markup.edgewall.org/`. They can affect how the template is rendered in a number of ways: Markup provides directives for conditionals and looping, among others. To use directives in a template, the namespace should be declared, which is usually done on the root element: {{{ #!xml ... }}} In this example, the default namespace is set to the XHTML namespace, and the namespace for Markup directives is bound to the prefix “py”. All directives can be applied as attributes, and some can also be used as elements. The `if` directives for conditionals, for example, can be used in both ways: {{{ #!xml ...

Bar

... }}} This is basically equivalent to the following: {{{ #!xml ...

Bar

... }}} The rationale behind the second form is that directives do not always map naturally to elements in the template. In such cases, the [wiki:MarkupTemplates#pystrip py:strip] directive can be used to strip off the unwanted element, or the directive can simply be used as an element. === `py:content` === This directive replaces any nested content with the result of evaluating the expression. {{{ #!xml }}} Given `bar='Bye'` in the context data, this would produce: {{{ #!xml }}} This directive can only be used as an attribute. === `py:replace` === This directive replaces the element itself with the result of evaluating the expression. {{{ #!xml
Hello
}}} Given `bar='Bye'` in the context data, this would produce: {{{ #!xml
Bye
}}} This directive can only be used as an attribute. === `py:attrs` === This directive adds, modifies or removes attributes from the element. {{{ #!xml }}} Given `foo={'class': 'collapse'}` in the template context, this would produce: {{{ #!xml }}} Attributes with the value `None` are omitted, so given `foo={'class': None}` in the context for the same template this would produce: {{{ #!xml }}} This directive can only be used as an attribute. === `py:strip` === This directive conditionally strips the top-level element from the output. When the value of the `py:strip` attribute evaluates to `True`, the element is stripped from the output: {{{ #!xml
foo
}}} This would be rendered as: {{{ #!xml
foo
}}} As a shorthand, if the value of the `py:strip` attribute is empty, that has the same effect as using a truth value (i.e. the element is stripped). === `py:if` === The element is only rendered if the expression evaluates to a truth value. {{{ #!xml
${bar}
}}} Given the data `foo=True` and `bar='Hello'` in the template context, this would produce: {{{ #!xml
Hello
}}} This directive can also be used as an element: {{{ #!xml
${bar}
}}} === `py:choose` / `py:when` / `py:otherwise` === This set of directives provides advanced contional processing for rendering one of several alternatives. The first matching `py:when` directive will be rendered or, if no `py:when` directive matches, the `py:otherwise` directive will be rendered. If the `py:choose` directive is empty the nested `py:when` directives will be tested for truth: {{{ #!xml
0 1 2
}}} This would produce the following output: {{{ #!xml
1
}}} If the `py:choose` directive contains an expression the nested `py:when` directives will be tested for equality to the parent `py:choose` value: {{{ #!xml
0 1 2
}}} This would produce the following output: {{{ #!xml
1
}}} === `py:for` === The element is repeated for every item in an iterable: {{{ #!xml }}} Given `items=[1, 2, 3]` in the context data, this would produce: {{{ #!xml }}} This directive can also be used as an element: {{{ #!xml }}} === `py:def` === The `py:def` directive can be used to create ''template functions'', i.e. snippets of template code that have a name and optionally some parameters, and that can be inserted in other places. {{{ #!xml

Hello, ${name}!

${greeting('world')} ${greeting('everyone else')}
}}} The above would be rendered to: {{{ #!xml

Hello, world!

Hello, everyone else!

}}} If a template function doesn't require parameters, it can be defined as well as called without the parenthesis. For example: {{{ #!xml

Hello, world!

${greeting}
}}} The above would be rendered to: {{{ #!xml

Hello, world!

}}} This directive can also be used as an element: {{{ #!xml

Hello, ${name}!

}}} === `py:with` === The `py:with` directive lets you assign expressions to variables, which can be used to make expressions inside the directive less verbose and more efficient. For example, if you need use the expression `author.posts` more than once, and that actually results in a database query, assigning the results to a variable using this directive would probably help. For example: {{{ #!xml
$x $y $z
}}} Given `x=42` in the context data, this would produce: {{{ #!xml
42 7 52
}}} This directive can also be used as an element: {{{ #!xml
$x $y $z
}}} Note that if a variable of the same name already existed outside of the scope of the `py:with` directive, it will '''not''' be overwritten. Instead, it will have the same value it had prior to the `py:with` assignment. Effectively, this means that variables are immutable in Markup. === `py:match` === This directive defines a ''match template'': given an XPath expression, it replaces any element in the template that matches the expression with its own content. For example, the match template defined in the following template matches any element with the tag name “greeting”: {{{ #!xml
Hello ${select('@name')}
}}} This would result in the following output: {{{ #!xml
Hello Dude
}}} Inside the body of a `py:match` directive, the `select(path)` function is made available so that parts or all of the original element can be incorporated in the output of the match template. See [wiki:MarkupStream#UsingXPath] for more information about this function. This directive can also be used as an element: {{{ #!xml
Hello ${select('@name')}
}}} == Template Includes == To reuse common snippets of template code, you can include other files using [http://www.w3.org/TR/xinclude/ XInclude]. For this, you need to declare the XInclude namespace (commonly bound to the prefix "xi") and use the `` element where you want the external file to be pulled in: {{{ ... }}} Include paths are relative to the filename of the template currently being processed. So if the example above was in the file "`myapp/index.html`" (relative to the template search path), the XInclude processor would look for the included file at "`myapp/base.html`". You can also use Unix-style relative paths, for example "`../base.html`" to look in the parent directory. Any content included this way is inserted into the generated output instead of the `` element. The included template sees the same [wiki:MarkupTemplates#TemplateContext context data]. [wiki:MarkupTemplates#py:match Match templates] and [wiki:MarkupTemplates#py:def template functions] in the included template are also available to the including template after the point it was included. By default, an error will be raised if an included file is not found. If that's not what you want, you can specify fallback content that should be used if the include fails. For example, to to make the include above fail silently, you'd write: {{{ }}} See the [http://www.w3.org/TR/xinclude/ XInclude specification] for more about fallback content. Note though that Markup currently only supports a small subset of XInclude.