Python 3 - Template strings instead of external template engine I don't know about you, but there is only one thing where I use templates on the backend side.
step
is a pure-Python module providing a very simple template engine withminimum syntax. It supports variable expansion, flow control and embedding ofPython code.Installation
Use pip:
or download the package from GitHub and run theinstall script:
Basic usage
A template is a string containing any kind of textual content and a set ofdirectives representing variables, control structures and blocks of Python code.
Variables are enclosed in
{{}}
and follow the same syntax rules as Pythonvariables; e.g.: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.:All text between
<%
and %>
is considered Python code; you can use thebuiltin echo()
function to output some text from within Python code blocks;e.g.:You can use the special function
isdef()
to perform some actions only if aname is defined in the template namespace; e.g.:The
setopt()
function allows you to enable options that modify the templateoutput; the only supported option is 'strip', which removes leading/trailingwhitespace, contiguous whitespace and empty lines and defaults to true; e.g.:The 'strip' option can also be given as a parameter during
Template
objectcreation; e.g.: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 areavailable at http://www.kernel-panic.it/programming/step/.
Tests
To run the test suite, just run
python setup.py test
.Credits
Copyright (c) 2012 Daniele Mazzocchio ([email protected]).Several improvements by Erki Suurjaak.
Licensed under the BSD license (see LICENSE.md file).