Templating languages
From WhyNotWiki
Templating languages edit (Category edit)
Contents |
[edit] Kid templating
Kid (http://kid-templating.org/).
Kid is a simple template language for XML based vocabularies written in Python. It was spawned as a result of a kinky love triangle between XSLT, TAL, and PHP. We believe many of the best features of these languages live on in Kid with much of the limitations and complexity stamped out (see WhatsBorrowed and WhatsDifferent). For more info on current and planned features and licensing information, see AboutKid.
WhatsBorrowed - Kid - Trac (http://www.kid-templating.org/trac/wiki/WhatsBorrowed).
[edit] Borrowed Functionality
This page describes various aspects of the Kid template language that were taken from other template languages.
[edit] TAL/ZPT
Like Zope's TAL, Kid is an [attribute language (category)]. That is, it defines an XML namespace and a set of attributes that can be used in other XML vocabularies to control content generation.
Kid resembles TAL more than any other template language. Some of the items that were taken from TAL include:
- py:for - based on TAL's tal:repeat.
- py:if - based on TAL's tal:condition.
- py:strip - based on TAL's tal:omit-tag.
- py:content and py:replace - based on TAL's attributes with the same name.
[edit] XSLT
XSL Transformations (XSLT) is an XML based language for transforming a source XML document to a destination document. Kid was originally authored to create a mix between TAL and XSLT. TAL has nice Python integration and a terse syntax but lacks expressiveness in some areas (e.g. METAL) while XSLT has no Python integration, a verbose syntax but many powerful features.
Some of the features borrowed from XSLT include:
- Attribute value interpolation was initially implemented exactly like XSLTs. Python expressions could be embedded in attribute value by enclosing them in curly braces ({}). This syntax was later changed to use a dollar sign plus curly braces (${}) for parity with other Python systems and the concept was extended to text content.
- py:match is an attempt to clone XSLT's match templates. XSLT's match templates are generally much more powerful but the same basic concepts apply.
One of the large benefits of using XSLT is that output is generally guaranteed to be well-formed XML (when using the XML output method). In most cases, Kid provides this same assurance.
[edit] PHP
PHP uses a <?php?> processing instruction, while Kid uses a <?python?> processing instruction.
One notable difference between the two systems is that <?python?> processing instructions cannot be used to generate any output. They are used purely to import functionality and define variables and functions.
[edit] Genshi
http://genshi.edgewall.org/wiki/Documentation/streams.html
A stream can be attained in a number of ways. It can be:
- the result of parsing XML or HTML text, or
- the result of selecting a subset of another stream using XPath, or
- programmatically generated.
For example, the functions XML() and HTML() can be used to convert literal XML or HTML text to a markup stream:
>>> from genshi import XML >>> stream = XML('<p class="intro">Some text and ' ... '<a href="http://example.org/">a link</a>.' ... '<br/></p>') >>> stream <genshi.core.Stream object at ...>The stream is the result of parsing the text into events. Each event is a tuple of the form (kind, data, pos), where:
- kind defines what kind of event it is (such as the start of an element, text, a comment, etc).
- data is the actual data associated with the event. How this looks depends on the event kind (see event kinds)
- pos is a (filename, lineno, column) tuple that describes where the event “comes from”.
Filters
One important feature of markup streams is that you can apply filters to the stream, either filters that come with Genshi, or your own custom filters.
A filter is simply a callable that accepts the stream as parameter, and returns the filtered stream:
def noop(stream): """A filter that doesn't actually do anything with the stream.""" for kind, data, pos in stream: yield kind, data, pos
Error on call to Template:cite web: Parameter url must be specified.
In addition to the XML-based template language, Genshi provides a simple text-based template language, intended for basic plain text generation needs. The language is similar to Cheetah or Velocity.
...
#if foo ${bar} #end Your items: #for item in items * ${item} #end #def greeting(name) Hello, ${name}! #end ${greeting('world')} ${greeting('everyone else')} Magic numbers! #with y=7; z=x+10 $x $y $z #end
[edit] Cheetah
"Cheetah is probably the most popular pure-python template language." [1]
http://cheetahtemplate.org/ Cheetah
[edit] Velocity
http://jakarta.apache.org/velocity/ Velocity
Text markup languages edit (Category edit)
