Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Simple Template Engine for Python

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 step
Octocat-spinner-32 .gitignore
Octocat-spinner-32 LICENSE.md
Octocat-spinner-32 MANIFEST.in
Octocat-spinner-32 README.md
Octocat-spinner-32 setup.py
README.md

step - Simple Template Engine for Python

step is a pure-Python module providing a very simple template engine with minimum syntax. It supports variable expansion, flow control and embedding of Python code.

Installation

Download the package from GitHub and run the install script:

# python setup.py install

Basic usage

A template is a string containing any kind of textual content and a set of directives representing variables, control structures and blocks of Python code.

Variables are enclosed in {{}} and follow the same syntax rules as Python variables; e.g.:

This is variable x: {{x}}
This is the third item of my_list: {{my_list[2]}}
This is not a variable: \{\{x\}\}

Flow control expressions are written like regular Python control structures, preceded by the % sign and must be closed by a %end<statement> tag; e.g.:

%if (x > 2):
    x is greater than 2
%else:
    x is {{x}}
%endif

All text between <% and %> is considered Python code; you can use the builtin echo() function to output some text from within Python code blocks; e.g.:

<%
import time
echo("Current timestamp is {}".format(time.time()))
%>
<\% This is not Python code %\>

You can use the special function isdef() to perform some actions only if a name is defined in the template namespace; e.g.:

%if isdef("my_var")
    my_var is {{my_var}}
%endif

The setopt() function allows you to enable options that modify the template output; the only supported option is 'strip', which removes leading/trailing whitespace, contiguous whitespace and empty lines and defaults to true; e.g.:

<%setopt("strip", True)%>

The 'strip' option can also be given as a parameter during Template object creation; e.g.:

tmpl = step.Template(TEMPLATE_STRING, strip=True)

A backslash at the end of a line will suppress the newline character.

Documentation

More examples and a detailed description of the module and its classes are available at http://www.kernel-panic.it/programming/step/.

Tests

To run the test suite, just run python setup.py test.

Credits

Copyright (c) 2012-2013 Daniele Mazzocchio (danix@kernel-panic.it). Several improvements by Erki Suurjaak.

Licensed under BSD license (see LICENSE.md file).

Something went wrong with that request. Please try again.