Skip to content
indention-based template engine, implemented in python
Python VimL
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.




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

Why to 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 on 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.


API is simple:

>>> import mint
>>> loader = mint.Loader('./templates', cache=True)
>>> namespace = dict(a='a', b='b')
>>> result = loder.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 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 do this automaticly for you. It escapes all text inside tags and attributes. Autoescaping can not be switched 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 tag (as text) and in attribute escapes by mint. And that is good, but sometimes wee need to insert html and do not escape it. For this purpose mint uses special class mint.Markup, which implements interface of __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 with out escaping. That is the prefered 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 }})

It is inline tags record.


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 provide 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 }}
                {{ content }}


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 it is natural behavior. And item is just global variable for slot loop_slot. But in this case better to provide item to slot obviosly:

    #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

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.

That's all folks!

Something went wrong with that request. Please try again.