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

Last change on this file since 31397 was 31397, checked in by Ivan Raikov, 5 years ago

ersatz: further updates to new line handling in lexer logic mode

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