Introducing Normal Template for Python

Posted by tkouts on 14 February 2010 | 0 Comments

Tags: template engine, normal template

Normal Template is a template engine originally developed by my friend George Moschovitis in JavaScript. What I really like about Normal Template is its simplicity, compactness and its clean JSON-ish approach.

Therefore, I decided to write a Python port and use it as an alternative out-of-the-box template engine for the upcoming release of Porcupine. Normal templates are safe, applicable to any non XML/HTML contexts and portable to any programming language.

Sample usage:


<h1>Hello {=name}</h1>

{:select profile}

<p>Your age is {=profile/age}</p>

{:reduce articles}
<li>{=title} - {=count}</li>
<li>No articles found</li>

{:if admin}
You have admin rights
You don't have admin rights

from normaltemplate import *

template_file = open('template.html')
src ='utf-8')

# compile template into a Python function
template = compile(src)

data = { // the data dictionary in JSON format
'name' : 'George',
'profile' : {
'age' : 34,
'gender' : 'M'
'articles' : [
{ 'title' : 'Hello world', 'count' : 34 },
{ 'title' : 'Another article', 'count' : 23 },
{ 'title' : 'The final', 'count' : 7 }
'admin': True

# calling the template with the data dictionary
# returns the rendered output

The code for py-normal-template is available on github at The original JavaScript implementation is available at

Post your comment


No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments

Clicky Web Analytics