source: project/release/4/ersatz/trunk/tests/tmpl/cheatsheet.tmpl @ 28550

Last change on this file since 28550 was 28550, checked in by Ivan Raikov, 7 years ago

ersatz: bug fixes in grammar of unary operators and added sexpr->tvalue, tvalue->sexpr procedures [thanks to Matt Gushee for bug reports]

File size: 5.2 KB
Line 
1{% extends "base.tmpl" %}
2
3{% block main %}
4
5include test
6============
7
8{% include "header.tmpl" %}
9
10binop
11======
12
131+1 = {{ 1 + 1 }}
141-1 = {{ 1 - 1 }}
152*4 = {{ 2 * 4 }}
164/2 = {{ 4 / 2 }}
178^3 = {{ 8 ** 3 }}
188%3 = {{ 8 % 3 }}
19not true = {{ !true }}
20not true(2) = {{ not true }}
21
22single quote string test
23=========================
24
25{% set single_quoted = 'hoge' %}
26single_quoted = {{single_quoted}}
27
28expand test
29===========
30
31{% set danger = "<script>alert(1)</script>" %}
32expand with escape = {{ danger }}
33expand with safe = {{ danger|safe }}
34
35set test
36=========
37
38set hoge = "ok"
39{% set hoge = "ok" %}
40now hoge = {{ hoge }}
41
42if test
43=======
44
45{% if hoge == "ok" %}
46value of hoge == "ok"
47{% else %}
48value of hoge != "ok"
49{% endif %}
50
51for test
52========
53
54
55{% for item in [1,2,3,4,5] %}
56{% set tmp = "hoge" %}
57<p>{{item}}</p>
58<p>{{loop.cycle(1,2,3)}}</p>
59<p>tmp = {{item}}</p>
60{% endfor %}
61
62{% for href, title in [ ("http://yahoo.co.jp", "yahoo japan") ] %}
63<a href="{{href}}">{{title}}</a>
64{% endfor %}
65
66{% for href, title in [("http://yahoo.co.jp", "yahoo japan"), ("http://google.co.jp", "google japan")] %}
67<a href="{{href}}">{{title}}</a>
68{% endfor %}
69
70{% for href, title in [("http://yahoo.co.jp", "yahoo japan"), ("http://google.co.jp", "google japan")] %}
71<a href="{{href}}">{{title}}</a>
72{% endfor %}
73
74obj test
75=========
76
77{% set obj = { age:10, name: 'aa' } %}
78
79name = {{obj.name}}
80age = {{obj.age}}
81obj["name"] = {{ obj["name"] }}
82obj["age"] = {{ obj["age"] }}
83
84filter test
85===========
86
87upper test:{{ "must be upper"|upper }}
88word count for "hoge hage hige" = {{ "hoge hage hige"|wordcount}}
89
90func test
91=========
92
93range(0,3) = {% for x in range(0,3) %}{{x}}{% endfor %}
94range(3,0) = {% for x in range(3,0) %}{{x}}{% endfor %}
95
96strlen("testtest") = {{ strlen("testtest") }}
97strlen("日本語") = {{ strlen("日本語") }}
98
99round floor of 1.5 = {{1.5|round("floor")|int}}
100round ceil of 1.5 = {{1.5|round("ceil")|int}}
101
102join(",", [1,2,3,4,5]) = {{ join(",", [1,2,3,4,5]) }}
103
104{% with long_list =  [10,20,30,40,50,60,70,80,90,100] %}
105{% for row in slice(4, long_list) %}
106  {% set y = loop.index %}
107  {% for col in row %}
108  {% set x = loop.index %}
109  {{x}},{{y}} = {{col}}
110  {% endfor %}
111{% endfor %}
112{% endwith %}
113
114
115filter tag test
116===============
117
118{% filter upper %}
119must be upper
120{% endfilter %}
121
122list expr
123=========
124
125{% for x in [1,2,3,4.5,"str"] %}{{x}}{% endfor %}
126{% for x in [] %}{{x}}{% endfor %}
127
128{{ join("-", [1,2,3]) }}
129
130{{ "{{" }}
131
132syntax test "is"
133================
134
1356 is divisibleby 4 = {{ 6 is divisibleby(4) }}
1366 is divisibleby 3 = {{ 6 is divisibleby(3) }}
1376 is divisibleby 2 = {{ 6 is divisibleby(2) }}
1386 is divisibleby 2 = {{ 6 is divisibleby 2 }}
1396 is divisibleby 3 via func = {{ divisibleby(3,6) }}
140
141macro test
142==========
143
144{% macro hoge_macro(i,j) %}
145{{i}},{{j}}
146{% endmacro %}
147
148{{ hoge_macro(10,20) }}
149
150{# at this point, delay_macro is not declared, but we can call it. #}
151{{ delay_macro(10,20) }}
152
153{% macro delay_macro(x,y) %}
154{{x}} {{ caller(1,2) }} {{y}}
155{% endmacro %}
156
157{% call(a,b) delay_macro("from", "to") %}
158inner text!
159args of call = {{a}},{{b}}
160macro name = {{delay_macro.name}}
161via caller = {{delay_macro.caller}}
162{% endcall %}
163
164{% macro subject_with_back(anchor, subject) %}
165<a name="#{{anchor}}"></a>
166<h2>{{subject}}</h2>
167{{ caller() }}
168<a href="#top">back to top</a>
169{% endmacro %}
170
171{% call subject_with_back("about", "about me") %}
172Hi, I'm a programmer living in Japan.
173{% endcall %}
174
175{% call subject_with_back("language", "favorite language") %}
176I love Scheme.
177{% endcall %}
178
179{% import "macro.tmpl" as testmac %}
180
181{{ testmac.test_macro(10,20) }}
182
183{% from "macro.tmpl" import test_macro, other_macro %}
184
185
186{{ test_macro(30,40) }}
187{{ other_macro(50,60) }}
188
189{% endblock main %}
190
191{% macro test_caller(x,y,z,age=120) %}
192this is test_caller
193age={{age}}
194class={{kwargs.class}}
195{{caller (x,y,z,kw_from_test_caller="yeah")}}
196{% endmacro %}
197
198
199{% call(a,b=100) test_caller(100,200,300,class="mytest") %}
200name={{test_caller.name}}
201caller={{test_caller.caller}}
202catch_kwargs={{test_caller.catch_kwargs}}
203catch_vargs={{test_caller.catch_vargs}}
204kw_from_test_caller={{kwargs.kw_from_test_caller}}
205{% endcall %}
206
207with statement test
208====================
209
210{% with foo = 10, hoge = 20 %}
211inner with foo = {{foo}}, hoge = {{hoge}}
212{% endwith %}
213
214after with foo = {{foo}}, hoge = {{hoge}}
215
216{% with %}
217{% set hige = 20 %}
218inner with hige = {{hige}}
219hige is defined = {{ hige is defined }}
220{% endwith %}
221
222after with hige = {{hige}}
223hige is defined = {{ hige is defined }}
224
225in expr test
226=============
227
2281 is in [1,2,3] = {{ 1 in [1,2,3] }}
229
230{% if 1 in [1,2,3] %}
231yes! 1 is in [1,2,3]
232{% endif %}
233
234{#
235
236raw statement test
237===================
238
239{% raw %}
240this is not expanded -> {{hoge}}
241this is not for loop -> {% for x in long_list %}hahaha{% endfor %}
242{% endraw %}
243
244#}
245
246autoescape test
247===============
248
249autoescape = true
250{% set script = "<script></script>" %}
251{% autoescape true %}
252{{ script }}
253{% endautoescape %}
254
255autoescape = false
256{% autoescape false %}
257{{ script }}
258{% endautoescape %}
259
260obj literal
261===========
262
263{% set person = {age:10, name:"taro"} %}
264
265person["age"] = {{ person["age"] }}
266person.age = {{ person.age }}
267person["name"] = {{ person["name"] }}
268person.name = {{ person.name }}
269
270eval
271====
272
273{% set source = "{% set evalue = 'from eval!' %}{{evalue}}" %}
274{{ eval(source) }}
Note: See TracBrowser for help on using the repository browser.