All Projects → pfalcon → Utemplate

pfalcon / Utemplate

Micro template engine in Python with low memory usage, designed for Pycopy, a minimalist Python dialect, but also compatible with other Pythons.

Programming Languages

python
139335 projects - #7 most used programming language
micropython
64 projects

Projects that are alternatives of or similar to Utemplate

Gorazor
Razor view engine for go
Stars: ✭ 772 (+2170.59%)
Mutual labels:  template-engine
Spring Velocity Support
An support project of legacy velocity based on Spring Framework
Stars: ✭ 22 (-35.29%)
Mutual labels:  template-engine
Easytemplatejs
小巧,纯粹,高性能的 JavaScript 模板引擎,支持浏览器, Node.js, Express, koa。Small, pure, high-performance JavaScript template engine, support Browser, Node.js, Express, Koa.
Stars: ✭ 14 (-58.82%)
Mutual labels:  template-engine
Bars
Bars is a lightweight high performance HTML aware templating engine. Bars emits DOM rather than DOM-strings, this means the DOM state is preserved even if data updates happen.
Stars: ✭ 5 (-85.29%)
Mutual labels:  template-engine
React email editor
This project is experimental! It's my attempt to create visual email template editor using React+Redux+etc... tools stack.
Stars: ✭ 19 (-44.12%)
Mutual labels:  template-engine
Concise.css
A CSS framework that's lightweight and easy-to-use. Give up the bloat. Stop tripping over your classes. Be Concise.
Stars: ✭ 941 (+2667.65%)
Mutual labels:  minimalist
Jet
Jet template engine
Stars: ✭ 756 (+2123.53%)
Mutual labels:  template-engine
Redzone
Lightweight C++ template engine with Jinja2-like syntax
Stars: ✭ 30 (-11.76%)
Mutual labels:  template-engine
Brevifolia Gridsome Forestry
A simple starter blog built with Gridsome & Forestry
Stars: ✭ 22 (-35.29%)
Mutual labels:  minimalist
Mvfsillva
My personal website
Stars: ✭ 13 (-61.76%)
Mutual labels:  minimalist
Minifuture
A monadic Future design pattern implementation in Swift
Stars: ✭ 16 (-52.94%)
Mutual labels:  minimalist
Crisscross
A Markdown-centric template engine for batch offline document generation.
Stars: ✭ 18 (-47.06%)
Mutual labels:  template-engine
Vscode Smarty
Smarty syntax highlight extension for Visual Studio Code
Stars: ✭ 10 (-70.59%)
Mutual labels:  template-engine
Selmer
A fast, Django inspired template system in Clojure.
Stars: ✭ 801 (+2255.88%)
Mutual labels:  template-engine
Treefrog Framework
TreeFrog Framework : High-speed C++ MVC Framework for Web Application
Stars: ✭ 885 (+2502.94%)
Mutual labels:  template-engine
Dot Dom
.dom is a tiny (512 byte) template engine that uses virtual DOM and some of react principles
Stars: ✭ 757 (+2126.47%)
Mutual labels:  template-engine
Templates
Simple PHP template engine which is easy to use
Stars: ✭ 26 (-23.53%)
Mutual labels:  template-engine
Gridly
⚡️ The minimal (~100-170 bytes) grid system for modern browsers.
Stars: ✭ 962 (+2729.41%)
Mutual labels:  minimalist
Figdice
FigDice PHP Templating Engine
Stars: ✭ 21 (-38.24%)
Mutual labels:  template-engine
Docxtemplater Link Module
⚓️ Hyperlink module for docxtemplater
Stars: ✭ 12 (-64.71%)
Mutual labels:  template-engine

utemplate

utemplate is a lightweight and memory-efficient template engine for Python, primarily intended for use with Pycopy, a lightweight Python implementation (https://github.com/pfalcon/pycopy).

utemplate syntax is roughly based on Django/Jinja2 syntax (e.g. {% if %}, {{var}}), but only the most needed features are offered (for example, "filters" are syntactic sugar for function calls, and so far are not planned to be implemented).

utemplate compiles templates to Python source code, specifically to a generator function which, being iterated over, produces consecutive parts (substrings) of a rendered template. This allows for minimal memory usage during template substitution (with Pycopy, it starts from mere hundreds of bytes). Generated Python code can be imported as a module directly, or a simple loader class is provided for convenience. There is also a loader class which will compile templates on the fly, if not already compiled (currently not automatically recompiled if changed, this is on TODO).

To test/manage templates, utemplate_util.py tool is provided. For example, to quickly try a template (assuming you are already in examples/ dir):

pycopy ../utemplate_util.py run squares.tpl

or

python3 ../utemplate_util.py run squares.tpl

Templates can take parameters (that's how dynamic content is generated). Template parameters are passed as arguments to a generator function produced from a template. They also can be passed on the utemplate_util.py command line (arguments will be treated as strings in this case, but can be of any types if called from your code):

pycopy ../utemplate_util.py run test1.tpl foo bar

Quick Syntax Reference

Evaluating Python expression, converting it to a string and outputting to rendered content:

  • {{<expr>}}

Where expr is an arbitrary Python expression - from a bare variable name, to function calls, yield from, await expressions.

Supported statements:

  • {% if %}, {% elif %}, {% else %}, {% endif %} - the usual "if" statement
  • {% for %}, {% endfor %} - the usual "for" statement
  • {% while %}, {% endwhile %} - the usual "while" statement
  • {% args var1, var2, ... %} - specify arguments to a template
  • {% set var = expr %} - assignment statement
  • {% include "name.tpl" %} - statically include another template
  • {% include {{name}} %} - dynamically include template whose name is stored in variable name.

Naming Conventions

The current conventions (may be adjusted in the future):

  • The recommended extension for templates is .tpl, e.g. example.tpl.
  • When template is compiled, dot (.) in its name is replaced with underscore (_) and .py appended, e.g. example_tpl.py. It thus can be imported with import example_tpl.
  • The name passed to {% include %} statement should be full name of a template with extension, e.g. {% include "example.tpl" %}.
  • For dynamic form of the include, a variable should similarly contain a full name of the template, e.g. {% set name = "example.tpl" %} / {% include {{name}} %}.

Examples

examples/squares.tpl as mentioned in the usage examples above has the following content:

{% args n=5 %}
{% for i in range(n) %}
| {{i}} | {{"%2d" % i ** 2}} |
{% endfor %}

More examples are available in the examples/ directory.

If you want to see a complete example web application which uses utemplate, refer to https://github.com/pfalcon/notes-pico .

License

utemplate is written and maintained by Paul Sokolovsky. It's available under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].