{{{ #!rst ============================ Markup XML Template Language ============================ Markup provides a simple XML-based template language that is heavily inspired by Kid_, which in turn was inspired by a number of existing template languages, namely XSLT_, TAL_, and PHP_. .. _kid: http://kid-templating.org/ .. _python: http://www.python.org/ .. _xslt: http://www.w3.org/TR/xslt .. _tal: http://www.zope.org/Wikis/DevSite/Projects/ZPT/TAL .. _php: http://www.php.net/ This document describes the template language and will be most useful as reference to those developing Markup templates. Templates are XML files of some kind (such as XHTML) that include processing directives_ (elements or attributes identified by a separate namespace) that affect how the template is rendered, and template expressions_ that are dynamically substituted by variable data. .. contents:: Contents :depth: 3 .. sectnum:: .. _`expressions`: -------------------- 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), and vice-versa (i.e. access attributes 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 .. _`directives`: ------------------- 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:: ... 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:: ...
Bar
Bar
Hello, ${name}!
${greeting('world')} ${greeting('everyone else')}Hello, world!
Hello, everyone else!
Hello, world!
${greeting}Hello, world!
Hello, ${name}!