Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/web2py/yatl
https://github.com/web2py/yatl
Last synced: 6 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/web2py/yatl
- Owner: web2py
- License: bsd-3-clause
- Created: 2019-02-18T02:34:29.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-05-08T01:15:52.000Z (over 1 year ago)
- Last Synced: 2024-10-30T19:01:40.161Z (14 days ago)
- Language: Python
- Size: 87.9 KB
- Stars: 22
- Watchers: 5
- Forks: 15
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Yet Another Template Language
This is the web2py template language described [here](http://web2py.com/books/default/chapter/29/05/the-views) 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, SPANexample = """
{{ for k in range(num): }}
{{=SPAN(k, _class='number')}} is {{if k % 2 == 0:}}even{{else:}}odd{{pass}}
{{ pass }}
"""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): }}{{=x}}{{ pass }}
-
{{= link('http://example.com/%s' % k) }}
{{ for k in range(num): }}
{{ pass }}
"""
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)
```