Yet Another Teplate Language (it has been around since 2007)
Project description
Yet Another Template Language
This is the web2py template language described here made available as stand alone package so it can be used anywhere.
Basically it is pure Python within "{{" ... "}}" delimiters and blocks are terminated with "pass" if termination is not obvious. There is no indentation constraints.
For example:
from yatl import render, SPAN
example = """
<div>
{{ for k in range(num): }}
<span>{{=SPAN(k, _class='number')}} is {{if k % 2 == 0:}}even{{else:}}odd{{pass}}</span>
{{ pass }}
</div>
"""
print(render(example, context=dict(num=10, SPAN=SPAN), delimiters="{{ }}"))
In the example SPAN is an optional helper. Output is escaped by default unless marked up with the XML helper as in {{=XML('1 < 2')}}. Note that the helpers included here are similar but not identical to the web2py ones. They are 99% compatible but the implementation is different.
Any Python expressions is allowed in templates, including function and class defintions:
example = """
{{ def link(x): }}<a href="{{=x}}">{{=x}}</a>{{ pass }}
<ul>
{{ for k in range(num): }}
<li>
{{= link('http://example.com/%s' % k) }}
</li>
{{ pass }}
</ul>
"""
print(render(example, context=dict(num=10), delimiters="{{ }}"))
Caching
If you implement a caching reader as the one below, you mak yatl even faster:
CACHE = {}
def reader(filename):
if filename in CACHE:
return CACHE[filename]
with open(filename) as fp;
CACHE[filename] = content = fp.read()
return content
output = yatl.render(reader(filename), path=path, reader=reader)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file yatl-20230507.3.tar.gz
.
File metadata
- Download URL: yatl-20230507.3.tar.gz
- Upload date:
- Size: 22.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7d60abff563d7eb680f4f97efb107b60b32f80061c45bf4fab15c4ea2a057e2 |
|
MD5 | aa1ee295477870a084a891f864fb6f0f |
|
BLAKE2b-256 | bd3b723a667a24512a299e1e139608e787c3b24b7819302c15c7aac09c3bec68 |
File details
Details for the file yatl-20230507.3-py3-none-any.whl
.
File metadata
- Download URL: yatl-20230507.3-py3-none-any.whl
- Upload date:
- Size: 19.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56858ff32747ac5763ffdbeba795471dd6e9e5d68f12b4468eaaf0412dea24d9 |
|
MD5 | 29dd665167e166800a30aebad2ad720e |
|
BLAKE2b-256 | 2e2059043804636e0dc544689d0af071a19ff14e0f7f2963db3e4f0bd8ca8534 |