-
Notifications
You must be signed in to change notification settings - Fork 49
/
templating.html.twig
402 lines (284 loc) · 9.58 KB
/
templating.html.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<div class="row">
<div class="span6">
<h3>Including partials and components</h3>
{% raw %}
<pre><code>{% include "Bundle:Controller:action" %}
<strong>in Symfony 2.1:</strong>
{% render "Bundle:Controller:action" with {"max" : 3} %}
<strong>in Symfony 2.2:</strong>
{{ render(controller("Bundle:Controller:action", {max :3})) }}
</code></pre>{% endraw %}
<h3>Links</h3>
{% raw %}
<pre><code><a href="{{ path('homepage') }}">Home<a/> //relative
<a href="{{ url('homepage') }}">Home<a/> //absolute
<a href="{{ path('show', {'id':article.id}) }}">Home</a>
</code></pre>{% endraw %}
<h3>Assets</h3>
{% raw %}
<pre><code><img src="{{ 'uploads/'~foto.url }}"/>
</code></pre>{% endraw %}
</div>
<div class="span6">
<h3>Debug variables in a template</h3>
{% raw %}
<pre><code>{{ dump(article) }}
</code></pre>{% endraw %}
<h3>Global TWIG variables</h3>
{% raw %}
<pre><code>app.security
app.user
app.request
app.request.get('foo') //get
app.request.request.get('foo') //post
app.session
app.environment
app.debug
</code></pre>{% endraw %}
</div>
</div>
<h3>TWIG TAGS</h3>
<div class="row">
<div class="span6">
<h4>block</h4>
<p>When a template uses inheritance and if you want to print a block multiple times, use the block function:</p>
{% raw %}
<pre><code><title>{% block title %}{% endblock %}</title>
<h1>{{ block('title') }}</h1>
{% block body %}{% endblock %}
</code></pre>{% endraw %}
<h4>parent</h4>
<p>When a template uses inheritance, it's possible to render the contents of the parent block when overriding a block by using the parent function:</p>
{% raw %}
<pre><code>{% extends "base.html" %}
{% block sidebar %}
<h3>Table Of Contents</h3>
...
{{ parent() }}
{% endblock %}
</code></pre>{% endraw %}
<h4>for</h4>
<p>Loop over each item in a sequence. For example, to display a list of users provided in a variable called users:</p>
{% raw %}
<pre><code><h1>Members</h1>
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
</code></pre>{% endraw %}
<h6>The loop variable</h6>
<p>Inside of a for loop block you can access some special variables:</p>
{% raw %}
<pre><code>Variable Description
loop.index The current iteration of the loop. (1 indexed)
loop.index0 The current iteration of the loop. (0 indexed)
loop.revindex The number of iterations from the end of the loop (1 indexed)
loop.revindex0 The number of iterations from the end of the loop (0 indexed)
loop.first True if first iteration
loop.last True if last iteration
loop.length The number of items in the sequence
loop.parent The parent context
</code></pre>{% endraw %}
{% raw %}
<pre><code>{% for user in users %}
{{ loop.index }} - {{ user.username }}
{% endfor %}
</code></pre>{% endraw %}
<h4>if</h4>
<p>The if statement in Twig is comparable with the if statements of PHP.</p>
<p>In the simplest form you can use it to test if an expression evaluates to true:</p>
{% raw %}
<pre><code>{% if online == false %}
<p>Our website is in maintenance mode. Please, come back later.</p>
{% endif %}
</code></pre>{% endraw %}
<h4>raw</h4>
<p>Everything inside raw tags won't be parsed.</p>
{% raw %}
<pre><code>{% raw %}
This variable {{foo}} won't be parsed as twig var.
{% endraw. %}</code></pre>{% endraw %}
</div>
<div class="span6">
<h4>set</h4>
<p>Inside code blocks you can also assign values to variables. Assignments use the set tag and can have multiple targets:</p>
{% raw %}
<pre><code>{% set foo = 'foo' %}
{% set foo = [1, 2] %}
{% set foo = {'foo': 'bar'} %}
{% set foo = 'foo' ~ 'bar' %}
{% set foo, bar = 'foo', 'bar' %}
</code></pre>{% endraw %}
<h4>filter</h4>
<p>Filter sections allow you to apply regular Twig filters on a block of template data. Just wrap the code in the special filter section:</p>
{% raw %}
<pre><code>{% filter upper %}
This text becomes uppercase
{% endfilter %}
</code></pre>{% endraw %}
<p>You can also chain filters:</p>
{% raw %}
<pre><code>{% filter lower|escape %}
<strong>SOME TEXT</strong>
{% endfilter %}
</code></pre>{% endraw %}
<h4>macro</h4>
<p>Macros are comparable with functions in regular programming languages. They are useful to put often used HTML idioms into reusable elements to not repeat yourself.</p>
<p>Here is a small example of a macro that renders a form element:</p>
{% raw %}
<pre><code>{% macro input(name, value, type, size) %}
<input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />
{% endmacro %}
</code></pre>{% endraw %}
<p>Macros differs from native PHP functions in a few ways:</p>
<ul>
<li>Default argument values are defined by using the default filter in the macro body;</li>
<li>Arguments of a macro are always optional.</li>
</ul>
<p>But as PHP functions, macros don't have access to the current template variables.</p>
<p>Macros can be defined in any template, and need to be "imported" before being used (see the documentation for the import tag for more information):</p>
{% raw %}
<pre><code>{% import "forms.html" as forms %}
</code></pre>{% endraw %}
<p>The above import call imports the "forms.html" file (which can contain only macros, or a template and some macros), and import the functions as items of the forms variable.</p>
<p>The macro can then be called at will:</p>
{% raw %}
<pre><code><p>{{ forms.input('username') }}</p>
<p>{{ forms.input('password', null, 'password') }}</p>
</code></pre>{% endraw %}
<p>If macros are defined and used in the same template, you can use the special _self variable to import them:</p>
{% raw %}
<pre><code>{% import _self as forms %}
<p>{{ forms.input('username') }}</p>
</code></pre>{% endraw %}
</div>
</div>
<h3>TWIG FILTERS</h3>
<div class="row">
<div class="span6">
<h4>date</h4>
{% raw %}
<pre><code>{{ post.published_at|date("m/d/Y") }}
{{ post.published_at|date("m/d/Y", "Europe/Paris") }}
</code></pre>{% endraw %}
<h4>date_modify</h4>
{% raw %}
<pre><code>{{ post.published_at|date_modify("+1 day")|date("m/d/Y") }}
</code></pre>{% endraw %}
<h4>format</h4>
{% raw %}
<pre><code>{{ "I like %s and %s."|format(foo, "bar") }}
</code></pre>{% endraw %}
<h4>replace</h4>
{% raw %}
<pre><code>{{ "I like %this% and %that%."|replace({'%this%': foo, '%that%': "bar"}
</code></pre>{% endraw %}
<h4>number_format</h4>
{% raw %}
<pre><code>{{ 200.35|number_format }}
{{ 9800.333|number_format(2, '.', ',') }}
</code></pre>{% endraw %}
<h4>url_encode</h4>
{% raw %}
<pre><code>{{ data|url_encode() }}
</code></pre>{% endraw %}
<h4>json_encode</h4>
{% raw %}
<pre><code>{{ data|json_encode() }}
</code></pre>{% endraw %}
<h4>convert_encoding</h4>
{% raw %}
<pre><code>{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
</code></pre>{% endraw %}
<h4>title</h4>
{% raw %}
<pre><code>{{ 'my first car'|title }}
{# outputs 'My First Car' #}
</code></pre>{% endraw %}
<h4>capitalize</h4>
{% raw %}
<pre><code>{{ 'my first car'|capitalize }}
</code></pre>{% endraw %}
<h4>nl2br</h4>
{% raw %}
<pre><code>{{ "I like Twig.\nYou will like it too."|nl2br }}
{# outputs
I like Twig.<br />
You will like it too.
#}
</code></pre>{% endraw %}
<h4>raw</h4>
{% raw %}
<pre><code>{{ var|raw }} {# var won't be escaped #}
</code></pre>{% endraw %}
<h4>trim</h4>
{% raw %}
<pre><code>{{ ' I like Twig.'|trim('.') }}
</code></pre>{% endraw %}
</div>
<div class="span6">
<h4>upper</h4>
{% raw %}
<pre><code>{{ 'welcome'|upper }}
</code></pre>{% endraw %}
<h4>lower</h4>
{% raw %}
<pre><code>{{ 'WELCOME'|lower }}
</code></pre>{% endraw %}
<h4>striptags</h4>
{% raw %}
<pre><code>{% some_html|striptags %}
</code></pre>{% endraw %}
<h4>join</h4>
{% raw %}
<pre><code>{{ [1, 2, 3]|join('|') }}
{# returns 1|2|3 #}
</code></pre>{% endraw %}
<h4>split</h4>
{% raw %}
<pre><code>{{ "one,two,three"|split(',') }}
{# returns ['one', 'two', 'three'] #}
</code></pre>{% endraw %}
<h4>reverse</h4>
{% raw %}
<pre><code>{% for use in users|reverse %}
...
{% endfor %}
</code></pre>{% endraw %}
<h4>abs</h4>
{% raw %}
<pre><code>{{ number|abs }}
</code></pre>{% endraw %}
<h4>length</h4>
{% raw %}
<pre><code>{% if users|length > 10 %}
...
{% endif %}
</code></pre>{% endraw %}
<h4>sort</h4>
{% raw %}
<pre><code>{% for use in users|sort %}
...
{% endfor %}
</code></pre>{% endraw %}
<h4>default</h4>
{% raw %}
<pre><code>{{ var|default('var is not defined') }}
</code></pre>{% endraw %}
<h4>keys</h4>
{% raw %}
<pre><code>{% for key in array|keys %}
...
{% endfor %}
</code></pre>{% endraw %}
<h4>escape</h4>
{% raw %}
<pre><code>{{ user.username|e }}
{# is equivalent to #}
{{ user.username|e('html') }}
{{ user.username|e('css') }}
{{ user.username|e('js') }}
</code></pre>{% endraw %}
</div>
</div>