A lightweight python templating engine.
Project description
Each template function is marked with the decorator @templet. Template functions will be rewritten to expand their document string as a template and return the string result. For example:
from templet import templet @templet def jumped(animal, body): "the $animal jumped over the $body." print(jumped('cow', 'moon'))
The template language understands the following forms:
$var |
inserts the value of the variable var |
${...} |
evaluates the expression and inserts the result |
${[...]} |
evaluates the list comprehension and inserts all the results |
${{...}} |
executes enclosed code; use out.append(text) to insert text |
In addition the following special codes are recognized:
$$ |
an escape for a single $ |
$ |
a line continuation (only at the end of the line) |
$( $. |
translates directly to $( and $. so jquery does not need escaping |
$/ $' $" |
also passed through so the end of a regex does not need escaping |
Template functions are compiled into code that accumulates a list of strings in a local variable ‘out’, and then returns the concatenation of them. If you want to do complicated computation, you can append to the out variable directly inside a ${{...}} block, for example:
@templet def single_cell_row(name, values): ''' <tr><td>$name</td><td>${{ for val in values: out.append(string(val)) }}</td></tr> '''
Features
simple, thus developer friendly
minimal implementation (one file, about 100 lines of code!)
no API apart from template syntax
error line numbers are reported as accurately as possible
speed
Templet has a history from 2007 and was inspired by Tomer Filiba’s Templite class