-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtwig-perversion-tests.twig
137 lines (119 loc) · 2.67 KB
/
twig-perversion-tests.twig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<h1> Twig-perversion tests</h1>
<p>twig code for testing twig-perversion</p>
<h2>return</h2>
{% macro testMacro() %}
{% return "test" %}
ignore me...
{% endmacro %}
{% set x = _self.testMacro() %}
x = {{x}}
{# x = test #}
<hr>
{% macro t2(v) %}
{% if v < 0 %}
{% return "negative" %}
ignore me
{% else %}
{% return "positive" %}
{% endif %}
ignore me
{% endmacro %}
negative: {{ _self.t2(-4)}}
positive: {{ _self.t2(4)}}
<hr>
{% macro double(n) %}
{% return n * 2 %}
ignore me
{% endmacro %}
double(4): {{_self.double(4)}}
{% macro triple(n) %}
{% return n + _self.double(n) %}
ignore me
{% endmacro %}
triple(4): {{_self.triple(4)}}
<hr>
{% macro recur(n) %}
{{n}}
{% if n > 1 %}
{{ _self.recur(n-1)}}
{% endif %}
{% endmacro %}
{{ _self.recur(5)}}
<hr>
{% macro fact(n) %}
{% if n == 1 %}
{% return 1 %}
{% endif %}
{% return n * _self.fact(n-1) %}
{% endmacro %}
1!: {{ _self.fact(1)}}
{% set fact5 = _self.fact(5) %}
5! = {{fact5}}
<h2>while loops</h2>
while loop indexes<br>
{% set x = 0 %}
{% while x < 5 %}
{{x}} loop: {{loop.index}} {{loop.index0}} {{loop.first ? 'first' : 'not first'}}<br>
{% set x = x + 1 %}
{% endwhile %}
<hr>
while with loop and loop.parent indexes<br>
{% set x = 0 %}
{% while x < 5 %}
{% set y = 0 %}
{% while y < 3 %}
loop: {{loop.index}} {{loop.index0}} {{loop.first ? 'first' : 'not first'}}
parent: {{loop.parent.loop.index}} {{loop.parent.loop.index0}} {{loop.parent.loop.first ? 'first': 'not'}}<br>
{% set y = y + 1 %}
{% endwhile %}
{% set x = x + 1 %}
{% endwhile %}
<hr>
while w/ continue on even:
{% set x = 0 %}
{% while x < 5 %}
{% set x = x + 1 %}
{{x}}
{% if x % 2 == 0 %} {% continue %} {% endif %}
odd
{% endwhile %}
<hr>
while w/ break on 3:
{% set x = 0 %}
{% while x < 5 %}
{{x}}
{% set x = x + 1 %}
{% if x == 3 %} {% break %} {% endif %}
..
{% endwhile %}
<hr>
<h4>numeric</h4>
12 ? {{ 12 is numeric ? 'Yes' : 'No' }}
{# Yes #}
-1.3 ? {{ '-1.3' is numeric ? 'Yes' : 'No' }}
{# Yes #}
0x539 ? {{ '0x539' is numeric ? 'Yes' : 'No'}}
{# No #}
<h4>return types</h4>
{% macro foo() %}
{# ... calculate someValue ... #}
{% set someValue = 'our return value' %}
{% return someValue %}
{% endmacro %}
{% macro bar() %}
{% set x = 0 %}
{% while x < 5 %}
x = {{x}}
{% set x = x + 1 %}
{% endwhile %}
{% return %}
{% endmacro %}
{% macro baz() %}
{% return 23 %}
{% endmacro %}
{{ _self.foo() }}
{{ _self.foo() is string ? 'foo is string' : 'foo is not string' }}<br>
{{ _self.bar() }}
{{ _self.bar() is string ? 'bar is string' : 'bar is not string' }}<br>
{{ _self.baz() }}
{{ _self.baz() is string ? 'baz is string' : 'baz is not string' }}<br>