Skip to main content

Simple indetion based template engine

Project description


mint - is small, fast and easy to use (x)html templates engine. Implemented with python language.

Why use mint?:

single python module

You can copy to your project package and use it.

minimalistic syntax

Your templates will become smaller and more readable due to indent based syntax.

works fast

mint uses ast python module from standard library (since python2.6, thank you Armin and co). So all templates compiles to optimized python byte code (during first call) which works fast.


mint knows about (x)html tags and attributes, so you get smart escaping. (There is a plan to implement html validation during rendering)

not standing in your way

mint does’t hide exceptions like some other template engines, and shows line in your template file where exception was raised

Template engine was inspired by haml (a template engine written in ruby), but some concepts were redisigned for simplification and adoptation to python world.

Home page: Issue tracker:


Simple API:

>>> import mint
>>> loader = mint.Loader('./templates', cache=True)
>>> namespace = dict(a='a', b='b')
>>> result = loader.get_template('').render(**namespace)

mint.Loader accepts names of directories and then search for template files by name provided in get_template(name) call.


mint syntax is based on indention, so you see the document structure and update document fast. You can move blocks of code and do not search for begining of parent tag and where it ends.


Just use @ character before tag name to render a tag:


Why mint does’t use % char, like haml do? I think that @ char is more readable and it is just third button on the keyboard, so you type it by one hand (without finger gymnastics). Next example shows tags structure:


Indented tags @head and @body are children for tag @html (parent tag).

Text is interpreted as text:

        Text of paragraph

So last example will be rendered as:

        Text of paragraph

By the way you can use short variant:

    @p Text of paragraph


To define attribute mint uses concept similar to method calls:

Previouse example will be rendered as:

<div id="content"></div>

To define multiple attributes mint uses (so called) chaining:


Previouse example will be rendered as:

<img alt="" src="/img/my_picture.png" />

Note that mint knows about selfclosed html tags.

Why do not use python dictionary declaration syntax instead? Something like {alt:"", src:"/img/my_picture.png"}

Because it is overloaded for html templating. “Chained-methods-call” like syntax uses less chars to type.

mint alows to set/append value of tag attribute somewhere inside tag:

    // set value of attribute

    // append value to attribute
    @+class( header)

will be rendered as:

<div class="header"></div>

<div class="main header"></div>

This is very handy when you need to set content of tag and it’s attributes based on some condition.


As you know there are some chars we need to escape in xml. And mint does this automatically for you. It escapes all text inside tags and attributes. Autoescaping can’t be turned off:

@a.href(/docs?type=1&published=true) docs
@p.class( ' " < > & )
    <div class="inside" />

Will be rendered as:

<a href="/docs?type=1&amp;published=true">docs</a>
<p class="&#39; &quot; &lt; &gt; &amp;">
    &lt;div class=&quot;inside&quot; /&gt;

python expressions

Of course, template engine without possibility to insert python expressions is unusable. So in mint you can do this with syntax similar to jinja2 or django:

        @title {{ doc.title }}
            Here we have content {{ doc.content }}

Under the hood mint calls unicode on python expression and escapes result.

Note that you can provide any valid python expression between tokens {{ }}. Also note that you can use limited subset of python __builtins__.

In mint templates expressions can be used inside text elements and inside attributes:

@p.class(title {{ doc.main_doc_class }}).id({{ }}) {{ doc.body }}

As you remember all content inserted in tags (as text) and in attributes is escaped by mint. And this is good, but sometimes you need to insert unescaped html. For this purpose mint uses special class mint.Markup, which implements __html__ method (this is something like convention). To insert html inside templates you need to mark your python variables with mint.Markup inside your python code.

In previous example if doc.body has html we need attribute body to return mint.Markup(html_string). And that html_string will be inserted in template without escaping. That is the preferred way to insert markup inside html template.

Also note that there are two contexts to insert markup - tag and attribute. In case of tag mint.Markup instances will be inserted without modifications. But if you attemted to insert markup in attribute it will be additionaly escaped.

For example we have such python code:

class Doc(object):
    def __init__(self, title, body):
        self.title = mint.Markup(title)
        self.body = mint.Markup(body)

doc = Doc('<b>title</b>', '<p>content of document</p>')

And such template:

    @p.class(title).title({{ doc.title }}) {{ doc.title }}
    {{ doc.body }}

The result will be:

<div class="doc">
    <p class="title" title="&gt;b&lt;title&gt;/b&lt;">
    <p>content of document</p>

This feature of mint is very handy.


In mint you can use python statement for:

    #for img in images:
        @li @img.src({{ img.file }})

Note that:

@li @img.src({{ img.file }})

is similar to:

    @img.src({{ img.file }})

This is inline tags notation.


Conditions are easy to write too:

#for doc in docs:
    #if != current_id:
        @a.href({{ url_for('doc', }}) {{ doc.title }}
    #elif doc.title == 'I need paragraph':
        @p {{ doc.title }}
        {{ doc.title }}


To comment a line use token //:

// In this div we provide content, yours C.O.

Xml comments are supported, use token --:

-- In this div we provide content, yours C.O.

to get:

<!-- In this div we provide content, yours C.O. -->
<div id="content"></div>

Sometimes you need to use special tokens in text, so if a line starts with token \ line is not interpreted by mint:

@p.class(title) Here we have title
\@p.class(title) Here we have title

Will provide:

<p class="title">Here we have title</p>
@p.class(title) Here we have title


Simplification of syntax provides ambiguity. But it is very handy sometimes. In mint templates you can write such things:

    #for image in images:
        @li.class(image) @img.alt().src({{ image.path }})

This simplification alows to write nested tags in one line, one by one. In previous example all img tags will be inside li.

Remember rule #1: This records:

are the same.

Rule #2: you can append text to and only to last tag when you use syntax simplification:

    #for doc in docs:
        @li @p.class(title) {{ doc.title }}
            @p.class(descr) {{ doc.description }}

li will be rendered as:

    <p class="title">...</p>
    <p class="descr">...</p>

Be careful when using syntax simplification.


mint uses slots to implement template inheritance. Slot is nothing more but python function that retuns markup. Slot can be defined and called anywhere in template:

        @title {{ title }}

            #def content():
                @p.class(title) {{ title }}
                {{ text }}


As you can see in previous example we define slot content and call it after that. During call of slot it’s content will be inserted in template. And if we need to insert different content in that place we should inherit and override content slot implementation:


#def content():
    #for item in news:
        @a.href({{ url_for('news-item', }}) {{ news.title }}

It is simple and powerful concept.

Slots are python functions, so they see all global variables passed to template and have own scope. This is very handy, because sometimes people have problems with such things in other templates engines.

For example we need a block inside for loop:

    #for item in items:


#def loop_slot():
    @p.class(title) {{ item.title }}
    @img.alt().src({{ item.image.path }})

For mint this is natural behavior. And item is just global variable for slot loop_slot. But in this case it’s better to provide item to slot explicitly:

    #for item in items:


#def loop_slot(item):
    @p.class(title) {{ item.title }}
    @img.alt().src({{ item.image.path }})

Also we can call base slot inside overrided slot. In our case base slot will point to slot with same name in our base template. __base__ variable points inside current slot scope to implementation of current slot in parent template:

-- somewhere in head tag
#def js():

#def js():

This example will results in:

<!-- somewhere in head tag -->
<script type="text/javascript" scr="/js/main.js"></script>
<script type="text/javascript" scr="/js/photos.js"></script>

Slots are plain python functions, slots returns Markup objects so we can pass slots or result of slot call to other slots.

And more. We can use slots outside of templates. Lets take from example with for loop:

>>> import mint
>>> t = mint.Loader('.').get_template('')
>>> loop_slot = t.slot('loop_slot')
>>> # lets take image somewhere
>>> item = images.get(1)
>>> loop_slot(item)
Markup(u'<p class="title">...</p><img alt="" src="..." />')

But sometimes slots needs global variables, you must provide such variables with kwargs in method slot(name, **globals) of Template object.


mint provides global variable utils which contains useful constants and helper functions.

Doctype declarations

  • utils.doctype.html_strict

  • utils.doctype.html_transitional

  • utils.doctype.xhtml_strict

  • utils.doctype.xhtml_transitional

Example of usage:

{{ utils.doctype.html_strict }}

Class mint.Markup is utils.markup (this is replacement for hack {{ var|safe }})

utils.loop is helper function to use with for statement. It takes iterable object and returns tuple of item and special object that consist of useful info for each iteration:

#for item, l in utils.loop(items):
    @a.href({{ item.url }})
        {{ item.title }} {{ (l.first, l.last, l.odd) }} {{ l.cycle('one', 'two', 'three') }}

In previous example l.cycle('one', 'two', 'three') will return one of values provided in sequence. It is handy to colorize tables.

Html helpers

  • utils.script

  • utils.scripts


Command Line Interface

mint has a CLI. To list available options use --help flag:

% python -m mint --help
Usage: [options] [template]

  -h, --help        show this help message and exit
  -c, --code        Show only python code of compiled template.
  -t, --tokenize    Show tokens stream of template.
  -r N, --repeat=N  Try to render template N times and display average time
  -p, --pprint      Turn pretty print on.
  -m, --monitor     Monitor current directory and subdirectories for changes
                    in mint files. And render corresponding html files.

CLI works in two modes:

  • rendering

  • monitoring

That’s all folks!



  • Smart indent. Tokenizer sets indent level for whole template equal indent level of first indented line (as in python)

  • Mint nodes now based on ast.AST and to transform mint tree to python tree we use ast.NodeTransformer subclass. This feature allows us write transformers for other languages (js =)

  • Added pretty printing of html result. Default indention is two spaces and is unchangable

  • Updated vim highlight for mint files

  • Added html helpers utils.script, and utils.scripts

  • New CLI interface. Some features

    • shows result python code listing for mint template

    • shows tokens stream for mint template

    • renders templates and prints result to stdout (note: if no outer variables used)

    • measures average rendering time for template (note: if no outer variables used)

    • monitoring feature

  • Fixes


Working implementation

Project details

Release history Release notifications | RSS feed

This version


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mint-0.5.tar.gz (20.5 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page