XML Template

XML Templates are inspired by web frameworks like Apache Wicket or Scala's Lift. A whole document or elements in a document are used as templates. Elements are also used as placeholders. And the last thing is that attributes can be filled by the template engine. The rest is done – according to the fundamental concept – by the program using Toxic.

Table of Contents

XML Namespace

To mark nested templates and placeholders some attributes are introduced into the template document. To avoid name collisions Toxic XML templates use the toxic XML namespace:

http://fractalqb.de/toxic/xml-template

Sometimes it may be inappropriate to put attributes into the XML template1. So if you really can't handle Toxic attributes in templates consider to use nested code templates.

Placeholders

There are three kinds of placeholders for XML templates: One to replace an entire XML element, one to fill content into an XML element – i.e. replace the child nodes – and placeholders for attributes.

TODO Replace Element

<?xml version="1.0"?>
<doc-root xmlns:toxic="http://fractalqb.de/toxic/xml-template">
  ...
  <div toxic:replace="replace-me" style="color:red;">
    The complete element including all its attributes
    and child nodes is replaced.
  </div>
  ...
</doc-root>

TODO Fill Element

<?xml version="1.0"?>
<doc-root xmlns:toxic="http://fractalqb.de/toxic/xml-template">
  ...
  <div toxic:fill="fill-me" style="color:red;">
    Attributes are kept but <em>all</em> child nodes
    are removed to be replaced with other content.
  </div>
  ...
</doc-root>

Attributes

The attributes to be filled or replaced are listed in a special toxic attribute of the affected XML element:

<?xml version="1.0"?>
<doc-root xmlns:toxic="http://fractalqb.de/toxic/xml-template">
  ...
  <div toxic:attributes="style=style-me align=alignment" style="color:red;">
    The <tt>toxic:attributes</tt> attribute defines a list of attributes
    that have to be filled with content.
  </div>
  ...
</doc-root>

The content of this special attribute toxic:attributes is a white space separated list of attribute/placeholder name pairs. I.e. in the example above the attribute style is replaced with content for the placeholder style-me and the attribute align will be filled with content for the placeholder alignment.

Nested Templates

TODO Template Element

<?xml version="1.0"?>
<doc-root xmlns:toxic="http://fractalqb.de/toxic/xml-template">
  ...
  <table toxic:template="table-template">
    A nested template that consists only of the tample element but not
    of the surrounding XML nodes.
  </table>
  ...
</doc-root>

TODO Template Node List

<?xml version="1.0"?>
<doc-root xmlns:toxic="http://fractalqb.de/toxic/xml-template"
          toxic:template-nodes="table-template">
  This text is the first node of the template. But the rest <em>is</em>
  also part of <b>this</b> template. However, the <tt>doc-root</tt>
  element is not.
</doc-root>

TODO Sub-Parser for Text Content

Footnotes:

1 Note the Toxic attributes won't make it into the output of template processing. So in general Toxic attributes shouldn't make trouble.

Author: Marcus Perlick

Date: May 25, 2014

HTML generated with emacs org-mode & Toxic by [qb]