You are on page 1of 1

TWI

G Cheat
Sheet Th
isd
ocu
men
tha
sbe
enc
rea
tedt
obes
har
ed!
Synt
ax Cont
rolSt
ruct
ure&oper
ator
s Li
nki
ngpages
{{ ... }} : Sayss
omething(pri
nt,ec
ho) St
ruc
tures:If, elseif, else, for Gener
atear elat
iveURLwithapar
ameter:
{% ... %} : Doessomething(i
f,
els
e,f
or,
etc
) Oper
ators <ahr
ef="
{{path(’
my r
out
e’,
{‘f
oo’
,‘
bar
’})}
}"
>..
.
</a>
{# This is a comment #} Mat +, -, /, %
hs: Gener
ateanabsolut
eURLwi t
houtparamet
er:
//:Dividesandretur
nstruncat
edresul
t,e.
g:10/
/4=2
Var
iabl
es **:Power ,e.
g:2**3 = 8
<ahr
ef="
{{ur
l(
’myr
oute’
)}}
">Myli
nk</
a>
Echoavari
abl
e: c:
Logi and, or, not, (expr), in, is Fi
lter
s
<h1>{{ title }}</h1> Compar i
s ons:==, !=, <, >, >=, <= Appl
yafilter orc
hainedfilters:
Arr
ayelement(bykey
/val
ue): Others:~ ( concat
enati
on), x..y (
range) {
{var
|s
tri
ptags}
} {
{var
|st
ri
ptags
|upper}
}
<em>{{ article.author }}</em> Examples: Somefil t
ers
Gl
obalVari
abl
es: {% for user in users %} for
mat,ti
tl
e,upper
,date,
nl2br
,l
ower,es
cape,r
aw,
{{ _self }} : c
urrenttemplat
e <li>{{ user.username }}</li> merge,
lengt
h, k
eys,s
li
ce,t
ri
m, s
ort
,capi
tal
iz
e,.
..
{{ _context }} : cur
rentcontext {% endfor %}
{% for i in 0..10 %}
Es
capi
ng
{{ _charset }} : cur
rentcharset
<li> N° {{ i }}</li> EscapeHTMLmanual
ly,Ali
asv
ers
ion:
Set
tingVar
iabl
es {% endfor %} {{c
ontent
|es
cape}
} {{c
ont
ent
|e}}
Si
mplevalue: Esc
apeHTMLaut omat
ical
ly:
{% for user in users if user.active %}
{% set foo = ‘bar’ %} {%autoes
c apet r
ue%}
<li>{{ user.username|e }}</li>
Anarray: {% endfor %} Heremyc ontenttoes
cape..
.
.
{% set foo = [1,2,3] %} {%endautoes c
ape%}
{% for user in users %}
Anarraywi
thkey/val
ue: <li>{{ user.username|e }}</li> Echot
herawval
ue:
{% set foo = {’foo’:‘bar’} %} {% else %} {{c
ont
ent
|r
aw}}
Wit
hac oncat
enedvari
able <li><em>no user found</em></li> Macr
os
{% set bar = foo ~ ‘ Hello’ %} {% endfor %} Why?Tonotr epeatyour s
elf
Setmul
ti
plevalues: Templ
atesi
nher
itanc
e Defineamac r
o:
{% set foo,bar = ‘foo’,‘bar’ %} {
%mac r
omak einput(
name, val
ue,type,s
ize
)%}
Defineablock:
Alongtext: <inputtype=" {
{type|
default(
't
ext'
)}}"
<divi
d=’

foot
er’

>
{% set foo %} name=" {
{name} }
"
{% block footer %}
Lorem ispsum dolores ... value=" {
{val
ue|
e} }
"
...
{% endset %} si
z e="{{si
ze|
defaul
t(20)}}
"/>
{% endblock %}
{
%endmac ro%}
I
ncl
udet
empl
ates </div>
Thedefiniti
onneedtobei mport
edt ousei
t:
I
ncl
udeat emplat
e Ext
endat empl
ate
{%import"
forms.
html
"asforms%}
{% include ‘sidebar.html’ %} {% extends ‘::base.html.twig’ %}
<p>
{{for
ms.makei
nput
('
username'
)}}
</p>
Pas
singthecont
ext(boxwil
lbeavai
blei
nsi
de) {% block footer %}
{% for box in boxes %} {# overriding here .. #}
{% include "render_box.html" %} {% endblock %}
{% endfor %} Cheat
Sheetpr edby@Thomas
opos _Qui
roga.
Exampl
esandT
wigl
ogoar
e© 2010Sens
ioLabs

You might also like