Package: tonnikala
TonnikalaTonnikala is the latest reincarnation among the Python templating
languages that feed on Kid-inspired XML syntax. It rejects the Kid
and Genshi notions of tagstreams and trees, and follows in
footsteps of Chameleon and Kajiki in making the template to compile
into Python bytecode directly. The syntax is very close to that of
Kajiki, but the internals are very different: Tonnikala writes code
as Abstract Syntax Trees and optimizes the resulting trees
extensively. In addition, there is an optional speed-up module
(currently Python 3 only), that provides a specialized class used
for output buffering.Examplesfromtonnikala.loaderimportLoadertemplate_source=u"""
<table>
    <tr py:for="row in table">
        <py:for each="key, value in row.items()"
            ><td>${key}</td><td>${literal(value)}</td></py:for>
    </tr>
</table>
"""template=Loader().load_string(template_source)ctx={'table':[dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10)forxinrange(1000)]}print(template.render(ctx))Template inheritancebase.tk<html><title><py:blockname="title_block">Iam${title}</py:block></title><py:deffunction="foo()">Icanbeoverriddentoo!</py:def><h1>${title_block()}</h1>${foo()}</html>child.tk<py:extendshref="base.tk"><py:blockname="title_block">ButIam${title}instead</py:block><py:deffunction="foo()">Ihaveoverriddenthefunctioninparenttemplate</py:def></py:extends>Template importsimportable.tk<html><py:deffunction="foo()">Iamanimportablefunction</py:def></html>importer.tk<html><py:importhref="importable.tk"alias="imp"/>${imp.foo()}</html>FileLoaderTo load templates from files, use thetonnikala.FileLoaderclass:loader=FileLoader(paths=['/path/to/templates'])template=loader.load('child.tk')AFileLoadercurrently implicitly cachesallloaded templates in memory.TemplateTo render the template:result=template.render(ctx)You can specify a block, or no-argument def to render explicitly:result=template.render(ctx,funcname='title_block')Pyramid integrationInclude‘tonnikala.pyramid’into your config to enable Tonnikala. When included, tonnikala adds the following configuration directives:add_tonnikala_extensions(*extensions)Registers tonnikala renderer for these template extensions. By default Tonnikala is not registered as a renderer for any extension.
For example:config.add_tonnikala_extensions(‘.html’, ‘.tk’)would enable Tonnikala renderer for templates with either of these extensions.add_tonnikala_search_paths(*paths)Adds the given paths to the end of Tonnikala search paths that are searched for templates. These can be absolute paths, orpackage.module:directory/subdirectory-style asset specs. By default no paths are searched explicitly (but you can always
use an asset spec for template.set_tonnikala_reload(reload)IfTrue, makes tonnikala not cache templates. Default isFalse.These 3 can also be controlled bytonnikala.extensions,tonnikala.search_pathsandtonnikala.reloadin .ini settings.StatusAlpha, working features areStructural elementspy:if,py:unless,py:def,py:for,py:replace,py:contentBasic template inheritance:py:extendsandpy:block; the child
template also inherits top level function declarations from the parent
template, and the child can override global functions that the parent
defines and uses.Expression interpolation using$simple_identifierand${complex + python + "expression"}Boolean attributes:<tagattr="${False}">,<tagattr="$True">Implicit escapingDisabling implicit escaping (literal())C speedups for both Python 2 and Python 3Importing def blocks from another template:py:importBasic I18N using gettext.Pyramid integrationUpcoming features:Structural elements:py:vars,py:switch,py:case;py:elseforfor,ifandswitch.Overriding attributes, setting attrs from dictionary:py:attrsCustom tags mapping topy:defI18N with optional in-parse-tree localization (partially done)Javascript as the target languagePluggable frontend syntax engines (partially done)METAL-like macrosPluggable expression languages akin to ChameleonEven better template inheritanceBetter documentationContributorsAntti Haapala