source: OpenRLabs-Git/deploy/rlabs-docker/web2py-rlabs/gluon/packages/yatl/README.md

main
Last change on this file was 42bd667, checked in by David Fuertes <dfuertes@…>, 4 years ago

Historial Limpio

  • Property mode set to 100755
File size: 1.6 KB
Line 
1# Yet Another Template Language
2
3This 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.
4
5Basically it is pure Python within "{{" ... "}}" delimiters and blocks are terminated with "pass" if termination is not obvious. There is no indentation constraints.
6
7For example:
8
9```
10from yatl import render, SPAN
11
12example = """
13<div> 
14{{ for k in range(num): }}
15<span>{{=SPAN(k, _class='number')}} is {{if k % 2 == 0:}}even{{else:}}odd{{pass}}</span>
16{{ pass }}
17</div>
18"""
19
20print(render(example, context=dict(num=10, SPAN=SPAN), delimiters="{{ }}"))
21```
22
23In the example SPAN is an optional helper.
24Output is escaped by default unless marked up with the XML helper as in {{=XML('1 < 2')}}.
25Note that the helpers included here are similar but not identical to the web2py ones.
26They are 99% compatible but the implementation is different.
27
28Any Python expressions is allowed in templates, including function and class defintions:
29
30```
31example = """
32{{ def link(x): }}<a href="{{=x}}">{{=x}}</a>{{ pass }}
33<ul>
34  {{ for k in range(num): }}
35  <li>
36     {{= link('http://example.com/%s' % k) }}
37  </li>
38  {{ pass }}
39</ul>
40"""
41
42print(render(example, context=dict(num=10), delimiters="{{ }}"))
43```
44
45## Caching
46
47If you implement a caching reader as the one below, you mak yatl even faster:
48
49```
50CACHE = {}
51def reader(filename):
52    if filename in CACHE:
53        return CACHE[filename]
54    with open(filename) as fp;
55        CACHE[filename] = content = fp.read()
56    return content
57     
58output = yatl.render(reader(filename), path=path, reader=reader)
59```
60
Note: See TracBrowser for help on using the repository browser.