TWI G Cheat Sheet
Th i sd o c u me n th a sb e e nc r e a t e dt ob es h a r e d!
Synt ax
Cont r olSt r uc t ur e&oper at or s
Li nki ngpages
{{ ... }} : Say ss omet hi ng( pr i nt , ec ho) {% ... %} : Doess omet hi ng( i f , el s e, f or , et c ) {# This is a comment #}
St r uc t ur es: If, elseif, else, for Oper at or s
Gener at ear el at i v eURLwi t hapar amet er: < ahr ef =" { {pat h( ’ my r out e’ , {‘ f oo’ , ‘ bar ’ })} } " >. . . < / a>
Var i abl es Ec hoavar i abl e: <h1>{{ title }}</h1> Ar r ayel ement( byk ey / val ue): <em>{{ article.author }}</em> Gl obalVar i abl es: {{ _self }} : c ur r entt empl at e {{ _context }} : c ur r entc ont ext {{ _charset }} : c ur r entc har s et
Set t i ngVar i abl es Si mpl eval ue: {% set foo = ‘bar’ %} Anar r ay: {% set foo = [1,2,3] %} Anar r aywi t hk ey / val ue: {% set foo = {’foo’:‘bar’} %} Wi t hac onc at enedvar i abl e {% set bar = foo ~ ‘ Hello’ %} Setmul t i pl eval ues: {% set foo,bar = ‘foo’,‘bar’ %} Al ongt ext: {% set foo %} Lorem ispsum dolores ... {% endset %}
I nc l udet empl at es I nc l udeat empl at e {% include ‘sidebar.html’ %} Pas s i ngt hec ont ext( boxwi l lbeavai bl ei ns i de) {% for box in boxes %} {% include "render_box.html" %} {% endfor %}
Mat hs: +, -, /, %
//: Di vi desandr et ur nst r unc at edr es ul t , e. g: 10/ / 4=2 **: Power , e. g: 2**3 = 8 Logi c: and, or, not, (expr), in, is Compar i s ons: ==, !=, <, >, >=, <= Ot her s:~ ( c onc at enat i on) , x..y ( r ange)
Exampl es: {% for user in users %} <li>{{ user.username }}</li> {% endfor %} {% for i in 0..10 %} <li> N° {{ i }}</li> {% endfor %} {% for user in users if user.active %} <li>{{ user.username|e }}</li> {% endfor %} {% for user in users %} <li>{{ user.username|e }}</li> {% else %} <li><em>no user found</em></li> {% endfor %}
Templ at esi nher i t anc e Defineabl oc k: < di vi d=’ ’ f oot er ’ ’ > {% block footer %} ... {% endblock %} </div> Ext endat empl at e {% extends ‘::base.html.twig’ %} {% block footer %} {# overriding here .. #} {% endblock %}
Gener at eanabs ol ut eURLwi t houtpar amet er: < ahr ef =" { {ur l ( ’ my r out e’ )} } " >Myl i nk< / a>
Fi l t er s Appl yafil t er { {v ar | s t r i pt ags} }
orc hai nedfil t er s: { {v ar | s t r i pt ags | upper} }
Somefil t er s f or mat , t i t l e, upper , dat e, nl 2 br , l ower , es c ape, r aw, mer ge, l engt h, k ey s , s l i c e, t r i m, s or t , c api t al i z e, . . .
Es c api ng Es c apeHTMLmanual l y ,Al i asv er s i on: { {c ont ent | es c ape} } { {c ont ent | e} } Es c apeHTMLaut omat i c al l y: { %aut oes c apet r ue%} Her emyc ont entt oes c ape. . . . { %endaut oes c ape%} Ec hot her awval ue: { {c ont ent | r aw} }
Mac r os Why?Tonotr epeaty our s el f Defineamac r o: { %mac r omak ei nput ( name, v al ue, t y pe, s i z e )%} < i nputt y pe=" { {t y pe| def aul t ( ' t e xt ' )} } " name=" { {name} } " v al ue=" { {v al ue| e} } " s i z e=" { {s i z e| def aul t ( 2 0)} } "/> { %endmac r o%} Thedefini t i onneedt obei mpor t edt ous ei t: { %i mpor t" f or ms . ht ml "asf or ms%} < p> { {f or ms . mak ei nput ( ' us er name' )} } < / p>
Cheat Sheetpr opos edby@Thomas _ Qui r oga. Exampl esandT wi gl ogoar e© 2010Sens i oLabs