utemplate - Lightweight template engine for MicroPython

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

utemplate - Lightweight template engine for MicroPython

Post by pfalcon » Sun Jun 01, 2014 4:45 pm

I worked on this project in deep background for a while, and finally found time to polish it a little bit and push to github/PyPI. The idea is to have lightweight, memory-efficient, unbloated template engine for MicroPython. Unbloated means there's no template interpreter - why to have it, if there's already interpreter in MicroPython, just need to convert template to Python source, voila. Memory-efficient means that partial chunk-by-chunk template rendering should be supported, so template is compiled into a generator function.

Quick start:

Code: Select all

git clone https://github.com/pfalcon/utemplate
cd utemplate/examples
micropython ../utemplate_util.py run squares.tpl
Example of template:

Code: Select all

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

Code: Select all

| 0 |  0 |
| 1 |  1 |
| 2 |  4 |
| 3 |  9 |
| 4 | 16 |
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: utemplate - Lightweight template engine for MicroPython

Post by pfalcon » Sun Jun 08, 2014 1:17 pm

Added include support:

Code: Select all

{% args %}
= Table of Squares =
{% include "squares.tpl" %}
====================
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: utemplate - Lightweight template engine for MicroPython

Post by pfalcon » Tue Jun 17, 2014 10:15 pm

0.2.6 was posted on PyPI: https://pypi.python.org/pypi/utemplate/0.2.6 . Stats say 1260 monthly downloads ;-).
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: utemplate - Lightweight template engine for MicroPython

Post by pfalcon » Tue Aug 12, 2014 10:06 pm

0.4 released on PyPI. Changes including dropping assumption of particular file extension for a template - any can be used now, which makes picoweb more compatible with Flask web microframework.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

bitninja
Posts: 165
Joined: Thu Sep 15, 2016 4:09 pm
Location: Spring, Texas

Re: utemplate - Lightweight template engine for MicroPython

Post by bitninja » Thu Nov 30, 2017 10:32 pm

Can the utemplate scripts be frozen into the ESP8266 build?

I have tried copying the compiled.py and source.py files into a sub-directory called utemplate in the \modules folder of the ESP8266 folder. But I am not sure that is the correct whey to install them.

I am definitely having issues running the examples that use the template rendering code.

Any ideas?

Thanks.

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: utemplate - Lightweight template engine for MicroPython

Post by pfalcon » Fri Dec 01, 2017 1:14 pm

Of course they can be, see viewtopic.php?f=16&t=2966&p=20150#p20150 .

And someone needs to tweak the stuff to make picoweb's examples, which originally intended to be run with unix port, to run on ports where there's no "external command line" too, see https://github.com/pfalcon/picoweb/issues/15 and linked issues.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: utemplate - Lightweight template engine for MicroPython

Post by pfalcon » Sat Feb 10, 2018 1:50 pm

utemplate 1.2 was released: https://pypi.python.org/pypi/utemplate/1.2

It contains experimental support for includes using a name stored in a variable:

Code: Select all

{% include {{inc_name}} %}
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

nodepythoner
Posts: 11
Joined: Sat Apr 20, 2019 8:44 am

Re: utemplate - Lightweight template engine for MicroPython

Post by nodepythoner » Thu May 09, 2019 2:11 pm

hello, how to use utemplate? i cant find documentation.

and i want to send variables to template, like here:
python code

Code: Select all

import picoweb
gc.collect()
app = picoweb.WebApp(__name__)
@app.route('/')
def index(req, resp):
   data='hello'
   yield from picoweb.start_response(resp)
   yield from app.render_template(resp,"index.html",(data,))


template

Code: Select all

<html>
<head>
</head>
<body>
{{data}}
</body>
</html>

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: utemplate - Lightweight template engine for MicroPython

Post by pfalcon » Wed Jun 12, 2019 7:01 am

utemplate 1.3 was released: https://pypi.org/project/utemplate/1.3/ .

It's mostly a documentation update, at least a quick, but syntax reference is now included, etc. The doc is now also rendered on PyPI.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Post Reply