/usr/share/doc/php-twig/manual/templates.html is in php-twig-doc 2.4.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Twig for Template Designers — php-twig-doc 2.4.6 documentation</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '2.4.6',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Twig for Developers" href="api.html" />
<link rel="prev" title="Installation" href="installation.html" />
</head>
<body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="api.html" title="Twig for Developers"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="installation.html" title="Installation"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">php-twig-doc 2.4.6 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="twig-for-template-designers">
<h1>Twig for Template Designers<a class="headerlink" href="#twig-for-template-designers" title="Permalink to this headline">¶</a></h1>
<p>This document describes the syntax and semantics of the template engine and
will be most useful as reference to those creating Twig templates.</p>
<div class="section" id="synopsis">
<h2>Synopsis<a class="headerlink" href="#synopsis" title="Permalink to this headline">¶</a></h2>
<p>A template is simply a text file. It can generate any text-based format (HTML,
XML, CSV, LaTeX, etc.). It doesn’t have a specific extension, <code class="docutils literal"><span class="pre">.html</span></code> or
<code class="docutils literal"><span class="pre">.xml</span></code> are just fine.</p>
<p>A template contains <strong>variables</strong> or <strong>expressions</strong>, which get replaced with
values when the template is evaluated, and <strong>tags</strong>, which control the logic
of the template.</p>
<p>Below is a minimal template that illustrates a few basics. We will cover further
details later on:</p>
<div class="highlight-html+jinja"><div class="highlight"><pre><span></span><span class="cp"><!DOCTYPE html></span>
<span class="p"><</span><span class="nt">html</span><span class="p">></span>
<span class="p"><</span><span class="nt">head</span><span class="p">></span>
<span class="p"><</span><span class="nt">title</span><span class="p">></span>My Webpage<span class="p"></</span><span class="nt">title</span><span class="p">></span>
<span class="p"></</span><span class="nt">head</span><span class="p">></span>
<span class="p"><</span><span class="nt">body</span><span class="p">></span>
<span class="p"><</span><span class="nt">ul</span> <span class="na">id</span><span class="o">=</span><span class="s">"navigation"</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">navigation</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">><</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">item.href</span> <span class="cp">}}</span><span class="s">"</span><span class="p">></span><span class="cp">{{</span> <span class="nv">item.caption</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">a</span><span class="p">></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
<span class="p"><</span><span class="nt">h1</span><span class="p">></span>My Webpage<span class="p"></</span><span class="nt">h1</span><span class="p">></span>
<span class="cp">{{</span> <span class="nv">a_variable</span> <span class="cp">}}</span>
<span class="p"></</span><span class="nt">body</span><span class="p">></span>
<span class="p"></</span><span class="nt">html</span><span class="p">></span>
</pre></div>
</div>
<p>There are two kinds of delimiters: <code class="docutils literal"><span class="pre">{%</span> <span class="pre">...</span> <span class="pre">%}</span></code> and <code class="docutils literal"><span class="pre">{{</span> <span class="pre">...</span> <span class="pre">}}</span></code>. The first
one is used to execute statements such as for-loops, the latter prints the
result of an expression to the template.</p>
</div>
<div class="section" id="ides-integration">
<h2>IDEs Integration<a class="headerlink" href="#ides-integration" title="Permalink to this headline">¶</a></h2>
<p>Many IDEs support syntax highlighting and auto-completion for Twig:</p>
<ul class="simple">
<li><em>Textmate</em> via the <a class="reference external" href="https://github.com/Anomareh/PHP-Twig.tmbundle">Twig bundle</a></li>
<li><em>Vim</em> via the <a class="reference external" href="http://jinja.pocoo.org/docs/integration/#vim">Jinja syntax plugin</a> or the <a class="reference external" href="https://github.com/lumiliet/vim-twig">vim-twig plugin</a></li>
<li><em>Netbeans</em> via the <a class="reference external" href="http://plugins.netbeans.org/plugin/37069/php-twig">Twig syntax plugin</a> (until 7.1, native as of 7.2)</li>
<li><em>PhpStorm</em> (native as of 2.1)</li>
<li><em>Eclipse</em> via the <a class="reference external" href="https://github.com/pulse00/Twig-Eclipse-Plugin">Twig plugin</a></li>
<li><em>Sublime Text</em> via the <a class="reference external" href="https://github.com/Anomareh/PHP-Twig.tmbundle">Twig bundle</a></li>
<li><em>GtkSourceView</em> via the <a class="reference external" href="https://github.com/gabrielcorpse/gedit-twig-template-language">Twig language definition</a> (used by gedit and other projects)</li>
<li><em>Coda</em> and <em>SubEthaEdit</em> via the <a class="reference external" href="https://github.com/bobthecow/Twig-HTML.mode">Twig syntax mode</a></li>
<li><em>Coda 2</em> via the <a class="reference external" href="https://github.com/muxx/Twig-HTML.mode">other Twig syntax mode</a></li>
<li><em>Komodo</em> and <em>Komodo Edit</em> via the Twig highlight/syntax check mode</li>
<li><em>Notepad++</em> via the <a class="reference external" href="https://github.com/Banane9/notepadplusplus-twig">Notepad++ Twig Highlighter</a></li>
<li><em>Emacs</em> via <a class="reference external" href="http://web-mode.org/">web-mode.el</a></li>
<li><em>Atom</em> via the <a class="reference external" href="https://github.com/reesef/php-twig">PHP-twig for atom</a></li>
<li><em>Visual Studio Code</em> via the <a class="reference external" href="https://marketplace.visualstudio.com/items?itemName=bajdzis.vscode-twig-pack">Twig pack</a></li>
</ul>
<p>Also, <a class="reference external" href="http://twigfiddle.com/">TwigFiddle</a> is an online service that allows you to execute Twig templates
from a browser; it supports all versions of Twig.</p>
</div>
<div class="section" id="variables">
<h2>Variables<a class="headerlink" href="#variables" title="Permalink to this headline">¶</a></h2>
<p>The application passes variables to the templates for manipulation in the
template. Variables may have attributes or elements you can access,
too. The visual representation of a variable depends heavily on the application providing
it.</p>
<p>You can use a dot (<code class="docutils literal"><span class="pre">.</span></code>) to access attributes of a variable (methods or
properties of a PHP object, or items of a PHP array), or the so-called
“subscript” syntax (<code class="docutils literal"><span class="pre">[]</span></code>):</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">foo.bar</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">foo</span><span class="o">[</span><span class="s1">'bar'</span><span class="o">]</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<p>When the attribute contains special characters (like <code class="docutils literal"><span class="pre">-</span></code> that would be
interpreted as the minus operator), use the <code class="docutils literal"><span class="pre">attribute</span></code> function instead to
access the variable attribute:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="c">{# equivalent to the non-working foo.data-foo #}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">attribute</span><span class="o">(</span><span class="nv">foo</span><span class="o">,</span> <span class="s1">'data-foo'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">It’s important to know that the curly braces are <em>not</em> part of the
variable but the print statement. When accessing variables inside tags,
don’t put the braces around them.</p>
</div>
<p>If a variable or attribute does not exist, you will receive a <code class="docutils literal"><span class="pre">null</span></code> value
when the <code class="docutils literal"><span class="pre">strict_variables</span></code> option is set to <code class="docutils literal"><span class="pre">false</span></code>; alternatively, if <code class="docutils literal"><span class="pre">strict_variables</span></code>
is set, Twig will throw an error (see <a class="reference internal" href="api.html#environment-options"><span class="std std-ref">environment options</span></a>).</p>
<div class="sidebar">
<p class="first sidebar-title">Implementation</p>
<p>For convenience’s sake <code class="docutils literal"><span class="pre">foo.bar</span></code> does the following things on the PHP
layer:</p>
<ul class="simple">
<li>check if <code class="docutils literal"><span class="pre">foo</span></code> is an array and <code class="docutils literal"><span class="pre">bar</span></code> a valid element;</li>
<li>if not, and if <code class="docutils literal"><span class="pre">foo</span></code> is an object, check that <code class="docutils literal"><span class="pre">bar</span></code> is a valid property;</li>
<li>if not, and if <code class="docutils literal"><span class="pre">foo</span></code> is an object, check that <code class="docutils literal"><span class="pre">bar</span></code> is a valid method
(even if <code class="docutils literal"><span class="pre">bar</span></code> is the constructor - use <code class="docutils literal"><span class="pre">__construct()</span></code> instead);</li>
<li>if not, and if <code class="docutils literal"><span class="pre">foo</span></code> is an object, check that <code class="docutils literal"><span class="pre">getBar</span></code> is a valid method;</li>
<li>if not, and if <code class="docutils literal"><span class="pre">foo</span></code> is an object, check that <code class="docutils literal"><span class="pre">isBar</span></code> is a valid method;</li>
<li>if not, and if <code class="docutils literal"><span class="pre">foo</span></code> is an object, check that <code class="docutils literal"><span class="pre">hasBar</span></code> is a valid method;</li>
<li>if not, return a <code class="docutils literal"><span class="pre">null</span></code> value.</li>
</ul>
<p><code class="docutils literal"><span class="pre">foo['bar']</span></code> on the other hand only works with PHP arrays:</p>
<ul class="last simple">
<li>check if <code class="docutils literal"><span class="pre">foo</span></code> is an array and <code class="docutils literal"><span class="pre">bar</span></code> a valid element;</li>
<li>if not, return a <code class="docutils literal"><span class="pre">null</span></code> value.</li>
</ul>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you want to access a dynamic attribute of a variable, use the
<a class="reference internal" href="functions/attribute.html"><span class="doc">attribute</span></a> function instead.</p>
</div>
<div class="section" id="global-variables">
<h3>Global Variables<a class="headerlink" href="#global-variables" title="Permalink to this headline">¶</a></h3>
<p>The following variables are always available in templates:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">_self</span></code>: references the current template name;</li>
<li><code class="docutils literal"><span class="pre">_context</span></code>: references the current context;</li>
<li><code class="docutils literal"><span class="pre">_charset</span></code>: references the current charset.</li>
</ul>
</div>
<div class="section" id="setting-variables">
<h3>Setting Variables<a class="headerlink" href="#setting-variables" title="Permalink to this headline">¶</a></h3>
<p>You can assign values to variables inside code blocks. Assignments use the
<a class="reference internal" href="tags/set.html"><span class="doc">set</span></a> tag:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">foo</span> <span class="o">=</span> <span class="s1">'foo'</span> <span class="cp">%}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">set</span> <span class="nv">foo</span> <span class="o">=</span> <span class="o">[</span><span class="m">1</span><span class="o">,</span> <span class="m">2</span><span class="o">]</span> <span class="cp">%}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">set</span> <span class="nv">foo</span> <span class="o">=</span> <span class="o">{</span><span class="s1">'foo'</span><span class="o">:</span> <span class="s1">'bar'</span><span class="o">}</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="filters">
<h2>Filters<a class="headerlink" href="#filters" title="Permalink to this headline">¶</a></h2>
<p>Variables can be modified by <strong>filters</strong>. Filters are separated from the
variable by a pipe symbol (<code class="docutils literal"><span class="pre">|</span></code>) and may have optional arguments in
parentheses. Multiple filters can be chained. The output of one filter is
applied to the next.</p>
<p>The following example removes all HTML tags from the <code class="docutils literal"><span class="pre">name</span></code> and title-cases
it:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">name</span><span class="o">|</span><span class="nf">striptags</span><span class="o">|</span><span class="nf">title</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<p>Filters that accept arguments have parentheses around the arguments. This
example will join a list by commas:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">list</span><span class="o">|</span><span class="nf">join</span><span class="o">(</span><span class="s1">', '</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<p>To apply a filter on a section of code, wrap it in the
<a class="reference internal" href="tags/filter.html"><span class="doc">filter</span></a> tag:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">filter</span> <span class="nf">upper</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> This text becomes uppercase</span>
<span class="cp">{%</span> <span class="k">endfilter</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<p>Go to the <a class="reference internal" href="filters/index.html"><span class="doc">filters</span></a> page to learn more about built-in
filters.</p>
</div>
<div class="section" id="functions">
<h2>Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h2>
<p>Functions can be called to generate content. Functions are called by their
name followed by parentheses (<code class="docutils literal"><span class="pre">()</span></code>) and may have arguments.</p>
<p>For instance, the <code class="docutils literal"><span class="pre">range</span></code> function returns a list containing an arithmetic
progression of integers:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">i</span> <span class="k">in</span> <span class="nv">range</span><span class="o">(</span><span class="m">0</span><span class="o">,</span> <span class="m">3</span><span class="o">)</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> </span><span class="cp">{{</span> <span class="nv">i</span> <span class="cp">}}</span><span class="x">,</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<p>Go to the <a class="reference internal" href="functions/index.html"><span class="doc">functions</span></a> page to learn more about the
built-in functions.</p>
</div>
<div class="section" id="named-arguments">
<h2>Named Arguments<a class="headerlink" href="#named-arguments" title="Permalink to this headline">¶</a></h2>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">i</span> <span class="k">in</span> <span class="nv">range</span><span class="o">(</span><span class="nv">low</span><span class="o">=</span><span class="m">1</span><span class="o">,</span> <span class="nv">high</span><span class="o">=</span><span class="m">10</span><span class="o">,</span> <span class="nv">step</span><span class="o">=</span><span class="m">2</span><span class="o">)</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> </span><span class="cp">{{</span> <span class="nv">i</span> <span class="cp">}}</span><span class="x">,</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<p>Using named arguments makes your templates more explicit about the meaning of
the values you pass as arguments:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">data</span><span class="o">|</span><span class="nf">convert_encoding</span><span class="o">(</span><span class="s1">'UTF-8'</span><span class="o">,</span> <span class="s1">'iso-2022-jp'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="c">{# versus #}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">data</span><span class="o">|</span><span class="nf">convert_encoding</span><span class="o">(</span><span class="nv">from</span><span class="o">=</span><span class="s1">'iso-2022-jp'</span><span class="o">,</span> <span class="nv">to</span><span class="o">=</span><span class="s1">'UTF-8'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<p>Named arguments also allow you to skip some arguments for which you don’t want
to change the default value:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="c">{# the first argument is the date format, which defaults to the global date format if null is passed #}</span><span class="x"></span>
<span class="cp">{{</span> <span class="s2">"now"</span><span class="o">|</span><span class="nf">date</span><span class="o">(</span><span class="nv">null</span><span class="o">,</span> <span class="s2">"Europe/Paris"</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="c">{# or skip the format value by using a named argument for the time zone #}</span><span class="x"></span>
<span class="cp">{{</span> <span class="s2">"now"</span><span class="o">|</span><span class="nf">date</span><span class="o">(</span><span class="nv">timezone</span><span class="o">=</span><span class="s2">"Europe/Paris"</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<p>You can also use both positional and named arguments in one call, in which
case positional arguments must always come before named arguments:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="s2">"now"</span><span class="o">|</span><span class="nf">date</span><span class="o">(</span><span class="s1">'d/m/Y H:i'</span><span class="o">,</span> <span class="nv">timezone</span><span class="o">=</span><span class="s2">"Europe/Paris"</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<div class="admonition tip">
<p class="first admonition-title">Tip</p>
<p class="last">Each function and filter documentation page has a section where the names
of all arguments are listed when supported.</p>
</div>
</div>
<div class="section" id="control-structure">
<h2>Control Structure<a class="headerlink" href="#control-structure" title="Permalink to this headline">¶</a></h2>
<p>A control structure refers to all those things that control the flow of a
program - conditionals (i.e. <code class="docutils literal"><span class="pre">if</span></code>/<code class="docutils literal"><span class="pre">elseif</span></code>/<code class="docutils literal"><span class="pre">else</span></code>), <code class="docutils literal"><span class="pre">for</span></code>-loops, as
well as things like blocks. Control structures appear inside <code class="docutils literal"><span class="pre">{%</span> <span class="pre">...</span> <span class="pre">%}</span></code>
blocks.</p>
<p>For example, to display a list of users provided in a variable called
<code class="docutils literal"><span class="pre">users</span></code>, use the <a class="reference internal" href="tags/for.html"><span class="doc">for</span></a> tag:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="x"><h1>Members</h1></span>
<span class="x"><ul></span>
<span class="x"> </span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">user</span> <span class="k">in</span> <span class="nv">users</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> <li></span><span class="cp">{{</span> <span class="nv">user.username</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="x"></li></span>
<span class="x"> </span><span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"></ul></span>
</pre></div>
</div>
<p>The <a class="reference internal" href="tags/if.html"><span class="doc">if</span></a> tag can be used to test an expression:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">users</span><span class="o">|</span><span class="nf">length</span> <span class="o">></span> <span class="m">0</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> <ul></span>
<span class="x"> </span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">user</span> <span class="k">in</span> <span class="nv">users</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> <li></span><span class="cp">{{</span> <span class="nv">user.username</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="x"></li></span>
<span class="x"> </span><span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> </ul></span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<p>Go to the <a class="reference internal" href="tags/index.html"><span class="doc">tags</span></a> page to learn more about the built-in tags.</p>
</div>
<div class="section" id="comments">
<h2>Comments<a class="headerlink" href="#comments" title="Permalink to this headline">¶</a></h2>
<p>To comment-out part of a line in a template, use the comment syntax <code class="docutils literal"><span class="pre">{#</span> <span class="pre">...</span>
<span class="pre">#}</span></code>. This is useful for debugging or to add information for other template
designers or yourself:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="c">{# note: disabled template because we no longer use this</span>
<span class="c"> {% for user in users %}</span>
<span class="c"> ...</span>
<span class="c"> {% endfor %}</span>
<span class="c">#}</span><span class="x"></span>
</pre></div>
</div>
</div>
<div class="section" id="including-other-templates">
<h2>Including other Templates<a class="headerlink" href="#including-other-templates" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="functions/include.html"><span class="doc">include</span></a> function is useful to include a template
and return the rendered content of that template into the current one:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">include</span><span class="o">(</span><span class="s1">'sidebar.html'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<p>By default, included templates have access to the same context as the template
which includes them. This means that any variable defined in the main template
will be available in the included template too:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">box</span> <span class="k">in</span> <span class="nv">boxes</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> </span><span class="cp">{{</span> <span class="nv">include</span><span class="o">(</span><span class="s1">'render_box.html'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<p>The included template <code class="docutils literal"><span class="pre">render_box.html</span></code> is able to access the <code class="docutils literal"><span class="pre">box</span></code> variable.</p>
<p>The filename of the template depends on the template loader. For instance, the
<code class="docutils literal"><span class="pre">Twig_Loader_Filesystem</span></code> allows you to access other templates by giving the
filename. You can access templates in subdirectories with a slash:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">include</span><span class="o">(</span><span class="s1">'sections/articles/sidebar.html'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<p>This behavior depends on the application embedding Twig.</p>
</div>
<div class="section" id="template-inheritance">
<h2>Template Inheritance<a class="headerlink" href="#template-inheritance" title="Permalink to this headline">¶</a></h2>
<p>The most powerful part of Twig is template inheritance. Template inheritance
allows you to build a base “skeleton” template that contains all the common
elements of your site and defines <strong>blocks</strong> that child templates can
override.</p>
<p>Sounds complicated but it is very basic. It’s easier to understand it by
starting with an example.</p>
<p>Let’s define a base template, <code class="docutils literal"><span class="pre">base.html</span></code>, which defines a simple HTML
skeleton document that you might use for a simple two-column page:</p>
<div class="highlight-html+jinja"><div class="highlight"><pre><span></span><span class="cp"><!DOCTYPE html></span>
<span class="p"><</span><span class="nt">html</span><span class="p">></span>
<span class="p"><</span><span class="nt">head</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">head</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">link</span> <span class="na">rel</span><span class="o">=</span><span class="s">"stylesheet"</span> <span class="na">href</span><span class="o">=</span><span class="s">"style.css"</span> <span class="p">/></span>
<span class="p"><</span><span class="nt">title</span><span class="p">></span><span class="cp">{%</span> <span class="k">block</span> <span class="nv">title</span> <span class="cp">%}{%</span> <span class="k">endblock</span> <span class="cp">%}</span> - My Webpage<span class="p"></</span><span class="nt">title</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">head</span><span class="p">></span>
<span class="p"><</span><span class="nt">body</span><span class="p">></span>
<span class="p"><</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">"content"</span><span class="p">></span><span class="cp">{%</span> <span class="k">block</span> <span class="nv">content</span> <span class="cp">%}{%</span> <span class="k">endblock</span> <span class="cp">%}</span><span class="p"></</span><span class="nt">div</span><span class="p">></span>
<span class="p"><</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">"footer"</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">footer</span> <span class="cp">%}</span>
<span class="ni">&copy;</span> Copyright 2011 by <span class="p"><</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"http://domain.invalid/"</span><span class="p">></span>you<span class="p"></</span><span class="nt">a</span><span class="p">></span>.
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
<span class="p"></</span><span class="nt">body</span><span class="p">></span>
<span class="p"></</span><span class="nt">html</span><span class="p">></span>
</pre></div>
</div>
<p>In this example, the <a class="reference internal" href="tags/block.html"><span class="doc">block</span></a> tags define four blocks that
child templates can fill in. All the <code class="docutils literal"><span class="pre">block</span></code> tag does is to tell the
template engine that a child template may override those portions of the
template.</p>
<p>A child template might look like this:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">"base.html"</span> <span class="cp">%}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">title</span> <span class="cp">%}</span><span class="x">Index</span><span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">head</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> </span><span class="cp">{{</span> <span class="nv">parent</span><span class="o">()</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> <style type="text/css"></span>
<span class="x"> .important { color: #336699; }</span>
<span class="x"> </style></span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">content</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> <h1>Index</h1></span>
<span class="x"> <p class="important"></span>
<span class="x"> Welcome to my awesome homepage.</span>
<span class="x"> </p></span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<p>The <a class="reference internal" href="tags/extends.html"><span class="doc">extends</span></a> tag is the key here. It tells the template
engine that this template “extends” another template. When the template system
evaluates this template, first it locates the parent. The extends tag should
be the first tag in the template.</p>
<p>Note that since the child template doesn’t define the <code class="docutils literal"><span class="pre">footer</span></code> block, the
value from the parent template is used instead.</p>
<p>It’s possible to render the contents of the parent block by using the
<a class="reference internal" href="functions/parent.html"><span class="doc">parent</span></a> function. This gives back the results of the
parent block:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">block</span> <span class="nv">sidebar</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> <h3>Table Of Contents</h3></span>
<span class="x"> ...</span>
<span class="x"> </span><span class="cp">{{</span> <span class="nv">parent</span><span class="o">()</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<div class="admonition tip">
<p class="first admonition-title">Tip</p>
<p class="last">The documentation page for the <a class="reference internal" href="tags/extends.html"><span class="doc">extends</span></a> tag describes
more advanced features like block nesting, scope, dynamic inheritance, and
conditional inheritance.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Twig also supports multiple inheritance with the so called horizontal reuse
with the help of the <a class="reference internal" href="tags/use.html"><span class="doc">use</span></a> tag. This is an advanced feature
hardly ever needed in regular templates.</p>
</div>
</div>
<div class="section" id="html-escaping">
<h2>HTML Escaping<a class="headerlink" href="#html-escaping" title="Permalink to this headline">¶</a></h2>
<p>When generating HTML from templates, there’s always a risk that a variable
will include characters that affect the resulting HTML. There are two
approaches: manually escaping each variable or automatically escaping
everything by default.</p>
<p>Twig supports both, automatic escaping is enabled by default.</p>
<p>The automatic escaping strategy can be configured via the
<a class="reference internal" href="api.html#environment-options"><span class="std std-ref">autoescape</span></a> option and defaults to <code class="docutils literal"><span class="pre">html</span></code>.</p>
<div class="section" id="working-with-manual-escaping">
<h3>Working with Manual Escaping<a class="headerlink" href="#working-with-manual-escaping" title="Permalink to this headline">¶</a></h3>
<p>If manual escaping is enabled, it is <strong>your</strong> responsibility to escape
variables if needed. What to escape? Any variable you don’t trust.</p>
<p>Escaping works by piping the variable through the
<a class="reference internal" href="filters/escape.html"><span class="doc">escape</span></a> or <code class="docutils literal"><span class="pre">e</span></code> filter:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">user.username</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<p>By default, the <code class="docutils literal"><span class="pre">escape</span></code> filter uses the <code class="docutils literal"><span class="pre">html</span></code> strategy, but depending on
the escaping context, you might want to explicitly use any other available
strategies:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">user.username</span><span class="o">|</span><span class="nf">e</span><span class="o">(</span><span class="s1">'js'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">user.username</span><span class="o">|</span><span class="nf">e</span><span class="o">(</span><span class="s1">'css'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">user.username</span><span class="o">|</span><span class="nf">e</span><span class="o">(</span><span class="s1">'url'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">user.username</span><span class="o">|</span><span class="nf">e</span><span class="o">(</span><span class="s1">'html_attr'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
</div>
<div class="section" id="working-with-automatic-escaping">
<h3>Working with Automatic Escaping<a class="headerlink" href="#working-with-automatic-escaping" title="Permalink to this headline">¶</a></h3>
<p>Whether automatic escaping is enabled or not, you can mark a section of a
template to be escaped or not by using the <a class="reference internal" href="tags/autoescape.html"><span class="doc">autoescape</span></a>
tag:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">autoescape</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> Everything will be automatically escaped in this block (using the HTML strategy)</span>
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<p>By default, auto-escaping uses the <code class="docutils literal"><span class="pre">html</span></code> escaping strategy. If you output
variables in other contexts, you need to explicitly escape them with the
appropriate escaping strategy:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">autoescape</span> <span class="s1">'js'</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> Everything will be automatically escaped in this block (using the JS strategy)</span>
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="escaping">
<h2>Escaping<a class="headerlink" href="#escaping" title="Permalink to this headline">¶</a></h2>
<p>It is sometimes desirable or even necessary to have Twig ignore parts it would
otherwise handle as variables or blocks. For example if the default syntax is
used and you want to use <code class="docutils literal"><span class="pre">{{</span></code> as raw string in the template and not start a
variable you have to use a trick.</p>
<p>The easiest way is to output the variable delimiter (<code class="docutils literal"><span class="pre">{{</span></code>) by using a variable
expression:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="s1">'{{'</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<p>For bigger sections it makes sense to mark a block
<a class="reference internal" href="tags/verbatim.html"><span class="doc">verbatim</span></a>.</p>
</div>
<div class="section" id="macros">
<h2>Macros<a class="headerlink" href="#macros" title="Permalink to this headline">¶</a></h2>
<p>Macros are comparable with functions in regular programming languages. They
are useful to reuse often used HTML fragments to not repeat yourself.</p>
<p>A macro is defined via the <a class="reference internal" href="tags/macro.html"><span class="doc">macro</span></a> tag. Here is a small example
(subsequently called <code class="docutils literal"><span class="pre">forms.html</span></code>) of a macro that renders a form element:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">macro</span> <span class="nv">input</span><span class="o">(</span><span class="nv">name</span><span class="o">,</span> <span class="nv">value</span><span class="o">,</span> <span class="nv">type</span><span class="o">,</span> <span class="nv">size</span><span class="o">)</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> <input type="</span><span class="cp">{{</span> <span class="nv">type</span><span class="o">|</span><span class="nf">default</span><span class="o">(</span><span class="s1">'text'</span><span class="o">)</span> <span class="cp">}}</span><span class="x">" name="</span><span class="cp">{{</span> <span class="nv">name</span> <span class="cp">}}</span><span class="x">" value="</span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="x">" size="</span><span class="cp">{{</span> <span class="nv">size</span><span class="o">|</span><span class="nf">default</span><span class="o">(</span><span class="m">20</span><span class="o">)</span> <span class="cp">}}</span><span class="x">" /></span>
<span class="cp">{%</span> <span class="k">endmacro</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<p>Macros can be defined in any template, and need to be “imported” via the
<a class="reference internal" href="tags/import.html"><span class="doc">import</span></a> tag before being used:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">import</span> <span class="s2">"forms.html"</span> <span class="k">as</span> <span class="nv">forms</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"><p></span><span class="cp">{{</span> <span class="nv">forms.input</span><span class="o">(</span><span class="s1">'username'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></p></span>
</pre></div>
</div>
<p>Alternatively, you can import individual macro names from a template into the
current namespace via the <a class="reference internal" href="tags/from.html"><span class="doc">from</span></a> tag and optionally alias them:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">from</span> <span class="s1">'forms.html'</span> <span class="k">import</span> <span class="nv">input</span> <span class="k">as</span> <span class="nv">input_field</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"><dl></span>
<span class="x"> <dt>Username</dt></span>
<span class="x"> <dd></span><span class="cp">{{</span> <span class="nv">input_field</span><span class="o">(</span><span class="s1">'username'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></dd></span>
<span class="x"> <dt>Password</dt></span>
<span class="x"> <dd></span><span class="cp">{{</span> <span class="nv">input_field</span><span class="o">(</span><span class="s1">'password'</span><span class="o">,</span> <span class="s1">''</span><span class="o">,</span> <span class="s1">'password'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></dd></span>
<span class="x"></dl></span>
</pre></div>
</div>
<p>A default value can also be defined for macro arguments when not provided in a
macro call:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">macro</span> <span class="nv">input</span><span class="o">(</span><span class="nv">name</span><span class="o">,</span> <span class="nv">value</span> <span class="o">=</span> <span class="s2">""</span><span class="o">,</span> <span class="nv">type</span> <span class="o">=</span> <span class="s2">"text"</span><span class="o">,</span> <span class="nv">size</span> <span class="o">=</span> <span class="m">20</span><span class="o">)</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> <input type="</span><span class="cp">{{</span> <span class="nv">type</span> <span class="cp">}}</span><span class="x">" name="</span><span class="cp">{{</span> <span class="nv">name</span> <span class="cp">}}</span><span class="x">" value="</span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="x">" size="</span><span class="cp">{{</span> <span class="nv">size</span> <span class="cp">}}</span><span class="x">" /></span>
<span class="cp">{%</span> <span class="k">endmacro</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<p>If extra positional arguments are passed to a macro call, they end up in the
special <code class="docutils literal"><span class="pre">varargs</span></code> variable as a list of values.</p>
</div>
<div class="section" id="expressions">
<span id="twig-expressions"></span><h2>Expressions<a class="headerlink" href="#expressions" title="Permalink to this headline">¶</a></h2>
<p>Twig allows expressions everywhere. These work very similar to regular PHP and
even if you’re not working with PHP you should feel comfortable with it.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>The operator precedence is as follows, with the lowest-precedence
operators listed first: <code class="docutils literal"><span class="pre">b-and</span></code>, <code class="docutils literal"><span class="pre">b-xor</span></code>, <code class="docutils literal"><span class="pre">b-or</span></code>, <code class="docutils literal"><span class="pre">or</span></code>, <code class="docutils literal"><span class="pre">and</span></code>,
<code class="docutils literal"><span class="pre">==</span></code>, <code class="docutils literal"><span class="pre">!=</span></code>, <code class="docutils literal"><span class="pre"><</span></code>, <code class="docutils literal"><span class="pre">></span></code>, <code class="docutils literal"><span class="pre">>=</span></code>, <code class="docutils literal"><span class="pre"><=</span></code>, <code class="docutils literal"><span class="pre">in</span></code>, <code class="docutils literal"><span class="pre">matches</span></code>,
<code class="docutils literal"><span class="pre">starts</span> <span class="pre">with</span></code>, <code class="docutils literal"><span class="pre">ends</span> <span class="pre">with</span></code>, <code class="docutils literal"><span class="pre">..</span></code>, <code class="docutils literal"><span class="pre">+</span></code>, <code class="docutils literal"><span class="pre">-</span></code>, <code class="docutils literal"><span class="pre">~</span></code>, <code class="docutils literal"><span class="pre">*</span></code>, <code class="docutils literal"><span class="pre">/</span></code>,
<code class="docutils literal"><span class="pre">//</span></code>, <code class="docutils literal"><span class="pre">%</span></code>, <code class="docutils literal"><span class="pre">is</span></code>, <code class="docutils literal"><span class="pre">**</span></code>, <code class="docutils literal"><span class="pre">|</span></code>, <code class="docutils literal"><span class="pre">[]</span></code>, and <code class="docutils literal"><span class="pre">.</span></code>:</p>
<div class="last highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">greeting</span> <span class="o">=</span> <span class="s1">'Hello '</span> <span class="cp">%}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">set</span> <span class="nv">name</span> <span class="o">=</span> <span class="s1">'Fabien'</span> <span class="cp">%}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">greeting</span> <span class="o">~</span> <span class="nv">name</span><span class="o">|</span><span class="nf">lower</span> <span class="cp">}}</span><span class="x"> </span><span class="c">{# Hello fabien #}</span><span class="x"></span>
<span class="c">{# use parenthesis to change precedence #}</span><span class="x"></span>
<span class="cp">{{</span> <span class="o">(</span><span class="nv">greeting</span> <span class="o">~</span> <span class="nv">name</span><span class="o">)|</span><span class="nf">lower</span> <span class="cp">}}</span><span class="x"> </span><span class="c">{# hello fabien #}</span><span class="x"></span>
</pre></div>
</div>
</div>
<div class="section" id="literals">
<h3>Literals<a class="headerlink" href="#literals" title="Permalink to this headline">¶</a></h3>
<p>The simplest form of expressions are literals. Literals are representations
for PHP types such as strings, numbers, and arrays. The following literals
exist:</p>
<ul>
<li><p class="first"><code class="docutils literal"><span class="pre">"Hello</span> <span class="pre">World"</span></code>: Everything between two double or single quotes is a
string. They are useful whenever you need a string in the template (for
example as arguments to function calls, filters or just to extend or include
a template). A string can contain a delimiter if it is preceded by a
backslash (<code class="docutils literal"><span class="pre">\</span></code>) – like in <code class="docutils literal"><span class="pre">'It\'s</span> <span class="pre">good'</span></code>. If the string contains a
backslash (e.g. <code class="docutils literal"><span class="pre">'c:\Program</span> <span class="pre">Files'</span></code>) escape it by doubling it
(e.g. <code class="docutils literal"><span class="pre">'c:\\Program</span> <span class="pre">Files'</span></code>).</p>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">42</span></code> / <code class="docutils literal"><span class="pre">42.23</span></code>: Integers and floating point numbers are created by just
writing the number down. If a dot is present the number is a float,
otherwise an integer.</p>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">["foo",</span> <span class="pre">"bar"]</span></code>: Arrays are defined by a sequence of expressions
separated by a comma (<code class="docutils literal"><span class="pre">,</span></code>) and wrapped with squared brackets (<code class="docutils literal"><span class="pre">[]</span></code>).</p>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">{"foo":</span> <span class="pre">"bar"}</span></code>: Hashes are defined by a list of keys and values
separated by a comma (<code class="docutils literal"><span class="pre">,</span></code>) and wrapped with curly braces (<code class="docutils literal"><span class="pre">{}</span></code>):</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="c">{# keys as string #}</span><span class="x"></span>
<span class="x">{ 'foo': 'foo', 'bar': 'bar' }</span>
<span class="c">{# keys as names (equivalent to the previous hash) #}</span><span class="x"></span>
<span class="x">{ foo: 'foo', bar: 'bar' }</span>
<span class="c">{# keys as integer #}</span><span class="x"></span>
<span class="x">{ 2: 'foo', 4: 'bar' }</span>
<span class="c">{# keys as expressions (the expression must be enclosed into parentheses) #}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">set</span> <span class="nv">foo</span> <span class="o">=</span> <span class="s1">'foo'</span> <span class="cp">%}</span><span class="x"></span>
<span class="x">{ (foo): 'foo', (1 + 1): 'bar', (foo ~ 'b'): 'baz' }</span>
</pre></div>
</div>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">true</span></code> / <code class="docutils literal"><span class="pre">false</span></code>: <code class="docutils literal"><span class="pre">true</span></code> represents the true value, <code class="docutils literal"><span class="pre">false</span></code>
represents the false value.</p>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">null</span></code>: <code class="docutils literal"><span class="pre">null</span></code> represents no specific value. This is the value returned
when a variable does not exist. <code class="docutils literal"><span class="pre">none</span></code> is an alias for <code class="docutils literal"><span class="pre">null</span></code>.</p>
</li>
</ul>
<p>Arrays and hashes can be nested:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">foo</span> <span class="o">=</span> <span class="o">[</span><span class="m">1</span><span class="o">,</span> <span class="o">{</span><span class="s2">"foo"</span><span class="o">:</span> <span class="s2">"bar"</span><span class="o">}]</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<div class="admonition tip">
<p class="first admonition-title">Tip</p>
<p class="last">Using double-quoted or single-quoted strings has no impact on performance
but string interpolation is only supported in double-quoted strings.</p>
</div>
</div>
<div class="section" id="math">
<h3>Math<a class="headerlink" href="#math" title="Permalink to this headline">¶</a></h3>
<p>Twig allows you to calculate with values. This is rarely useful in templates
but exists for completeness’ sake. The following operators are supported:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">+</span></code>: Adds two objects together (the operands are casted to numbers). <code class="docutils literal"><span class="pre">{{</span>
<span class="pre">1</span> <span class="pre">+</span> <span class="pre">1</span> <span class="pre">}}</span></code> is <code class="docutils literal"><span class="pre">2</span></code>.</li>
<li><code class="docutils literal"><span class="pre">-</span></code>: Subtracts the second number from the first one. <code class="docutils literal"><span class="pre">{{</span> <span class="pre">3</span> <span class="pre">-</span> <span class="pre">2</span> <span class="pre">}}</span></code> is
<code class="docutils literal"><span class="pre">1</span></code>.</li>
<li><code class="docutils literal"><span class="pre">/</span></code>: Divides two numbers. The returned value will be a floating point
number. <code class="docutils literal"><span class="pre">{{</span> <span class="pre">1</span> <span class="pre">/</span> <span class="pre">2</span> <span class="pre">}}</span></code> is <code class="docutils literal"><span class="pre">{{</span> <span class="pre">0.5</span> <span class="pre">}}</span></code>.</li>
<li><code class="docutils literal"><span class="pre">%</span></code>: Calculates the remainder of an integer division. <code class="docutils literal"><span class="pre">{{</span> <span class="pre">11</span> <span class="pre">%</span> <span class="pre">7</span> <span class="pre">}}</span></code> is
<code class="docutils literal"><span class="pre">4</span></code>.</li>
<li><code class="docutils literal"><span class="pre">//</span></code>: Divides two numbers and returns the floored integer result. <code class="docutils literal"><span class="pre">{{</span> <span class="pre">20</span>
<span class="pre">//</span> <span class="pre">7</span> <span class="pre">}}</span></code> is <code class="docutils literal"><span class="pre">2</span></code>, <code class="docutils literal"><span class="pre">{{</span> <span class="pre">-20</span>  <span class="pre">//</span> <span class="pre">7</span> <span class="pre">}}</span></code> is <code class="docutils literal"><span class="pre">-3</span></code> (this is just syntactic
sugar for the <a class="reference internal" href="filters/round.html"><span class="doc">round</span></a> filter).</li>
<li><code class="docutils literal"><span class="pre">*</span></code>: Multiplies the left operand with the right one. <code class="docutils literal"><span class="pre">{{</span> <span class="pre">2</span> <span class="pre">*</span> <span class="pre">2</span> <span class="pre">}}</span></code> would
return <code class="docutils literal"><span class="pre">4</span></code>.</li>
<li><code class="docutils literal"><span class="pre">**</span></code>: Raises the left operand to the power of the right operand. <code class="docutils literal"><span class="pre">{{</span> <span class="pre">2</span> <span class="pre">**</span>
<span class="pre">3</span> <span class="pre">}}</span></code> would return <code class="docutils literal"><span class="pre">8</span></code>.</li>
</ul>
</div>
<div class="section" id="logic">
<h3>Logic<a class="headerlink" href="#logic" title="Permalink to this headline">¶</a></h3>
<p>You can combine multiple expressions with the following operators:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">and</span></code>: Returns true if the left and the right operands are both true.</li>
<li><code class="docutils literal"><span class="pre">or</span></code>: Returns true if the left or the right operand is true.</li>
<li><code class="docutils literal"><span class="pre">not</span></code>: Negates a statement.</li>
<li><code class="docutils literal"><span class="pre">(expr)</span></code>: Groups an expression.</li>
</ul>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Twig also support bitwise operators (<code class="docutils literal"><span class="pre">b-and</span></code>, <code class="docutils literal"><span class="pre">b-xor</span></code>, and <code class="docutils literal"><span class="pre">b-or</span></code>).</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Operators are case sensitive.</p>
</div>
</div>
<div class="section" id="comparisons">
<h3>Comparisons<a class="headerlink" href="#comparisons" title="Permalink to this headline">¶</a></h3>
<p>The following comparison operators are supported in any expression: <code class="docutils literal"><span class="pre">==</span></code>,
<code class="docutils literal"><span class="pre">!=</span></code>, <code class="docutils literal"><span class="pre"><</span></code>, <code class="docutils literal"><span class="pre">></span></code>, <code class="docutils literal"><span class="pre">>=</span></code>, and <code class="docutils literal"><span class="pre"><=</span></code>.</p>
<p>You can also check if a string <code class="docutils literal"><span class="pre">starts</span> <span class="pre">with</span></code> or <code class="docutils literal"><span class="pre">ends</span> <span class="pre">with</span></code> another
string:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="s1">'Fabien'</span> <span class="nv">starts</span> <span class="k">with</span> <span class="s1">'F'</span> <span class="cp">%}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">if</span> <span class="s1">'Fabien'</span> <span class="nv">ends</span> <span class="k">with</span> <span class="s1">'n'</span> <span class="cp">%}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>For complex string comparisons, the <code class="docutils literal"><span class="pre">matches</span></code> operator allows you to use
<a class="reference external" href="http://php.net/manual/en/pcre.pattern.php">regular expressions</a>:</p>
<div class="last highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">phone</span> <span class="nv">matches</span> <span class="s1">'/^[\\d\\.]+$/'</span> <span class="cp">%}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="containment-operator">
<h3>Containment Operator<a class="headerlink" href="#containment-operator" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal"><span class="pre">in</span></code> operator performs containment test.</p>
<p>It returns <code class="docutils literal"><span class="pre">true</span></code> if the left operand is contained in the right:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="c">{# returns true #}</span><span class="x"></span>
<span class="cp">{{</span> <span class="m">1</span> <span class="k">in</span> <span class="o">[</span><span class="m">1</span><span class="o">,</span> <span class="m">2</span><span class="o">,</span> <span class="m">3</span><span class="o">]</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{{</span> <span class="s1">'cd'</span> <span class="k">in</span> <span class="s1">'abcde'</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<div class="admonition tip">
<p class="first admonition-title">Tip</p>
<p class="last">You can use this filter to perform a containment test on strings, arrays,
or objects implementing the <code class="docutils literal"><span class="pre">Traversable</span></code> interface.</p>
</div>
<p>To perform a negative test, use the <code class="docutils literal"><span class="pre">not</span> <span class="pre">in</span></code> operator:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="m">1</span> <span class="k">not</span> <span class="k">in</span> <span class="o">[</span><span class="m">1</span><span class="o">,</span> <span class="m">2</span><span class="o">,</span> <span class="m">3</span><span class="o">]</span> <span class="cp">%}</span><span class="x"></span>
<span class="c">{# is equivalent to #}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">if</span> <span class="k">not</span> <span class="o">(</span><span class="m">1</span> <span class="k">in</span> <span class="o">[</span><span class="m">1</span><span class="o">,</span> <span class="m">2</span><span class="o">,</span> <span class="m">3</span><span class="o">])</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
</div>
<div class="section" id="test-operator">
<h3>Test Operator<a class="headerlink" href="#test-operator" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal"><span class="pre">is</span></code> operator performs tests. Tests can be used to test a variable against
a common expression. The right operand is name of the test:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="c">{# find out if a variable is odd #}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">name</span> <span class="k">is</span> <span class="nf">odd</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<p>Tests can accept arguments too:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">post.status</span> <span class="k">is</span> <span class="nf">constant</span><span class="o">(</span><span class="s1">'Post::PUBLISHED'</span><span class="o">)</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<p>Tests can be negated by using the <code class="docutils literal"><span class="pre">is</span> <span class="pre">not</span></code> operator:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">post.status</span> <span class="k">is</span> <span class="k">not</span> <span class="nf">constant</span><span class="o">(</span><span class="s1">'Post::PUBLISHED'</span><span class="o">)</span> <span class="cp">%}</span><span class="x"></span>
<span class="c">{# is equivalent to #}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">if</span> <span class="k">not</span> <span class="o">(</span><span class="nv">post.status</span> <span class="k">is</span> <span class="nf">constant</span><span class="o">(</span><span class="s1">'Post::PUBLISHED'</span><span class="o">))</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<p>Go to the <a class="reference internal" href="tests/index.html"><span class="doc">tests</span></a> page to learn more about the built-in
tests.</p>
</div>
<div class="section" id="other-operators">
<h3>Other Operators<a class="headerlink" href="#other-operators" title="Permalink to this headline">¶</a></h3>
<p>The following operators don’t fit into any of the other categories:</p>
<ul>
<li><p class="first"><code class="docutils literal"><span class="pre">|</span></code>: Applies a filter.</p>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">..</span></code>: Creates a sequence based on the operand before and after the operator
(this is just syntactic sugar for the <a class="reference internal" href="functions/range.html"><span class="doc">range</span></a> function):</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="m">1.</span><span class="nv">.5</span> <span class="cp">}}</span><span class="x"></span>
<span class="c">{# equivalent to #}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">range</span><span class="o">(</span><span class="m">1</span><span class="o">,</span> <span class="m">5</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<p>Note that you must use parentheses when combining it with the filter operator
due to the <a class="reference internal" href="#twig-expressions"><span class="std std-ref">operator precedence rules</span></a>:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="x">(1..5)|join(', ')</span>
</pre></div>
</div>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">~</span></code>: Converts all operands into strings and concatenates them. <code class="docutils literal"><span class="pre">{{</span> <span class="pre">"Hello</span>
<span class="pre">"</span> <span class="pre">~</span> <span class="pre">name</span> <span class="pre">~</span> <span class="pre">"!"</span> <span class="pre">}}</span></code> would return (assuming <code class="docutils literal"><span class="pre">name</span></code> is <code class="docutils literal"><span class="pre">'John'</span></code>) <code class="docutils literal"><span class="pre">Hello</span>
<span class="pre">John!</span></code>.</p>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">.</span></code>, <code class="docutils literal"><span class="pre">[]</span></code>: Gets an attribute of an object.</p>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">?:</span></code>: The ternary operator:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span>{{ foo ? 'yes' : 'no' }}
{{ foo ?: 'no' }} is the same as {{ foo ? foo : 'no' }}
{{ foo ? 'yes' }} is the same as {{ foo ? 'yes' : '' }}
</pre></div>
</div>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">??</span></code>: The null-coalescing operator:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span>{# returns the value of foo if it is defined and not null, 'no' otherwise #}
{{ foo ?? 'no' }}
</pre></div>
</div>
</li>
</ul>
</div>
<div class="section" id="string-interpolation">
<h3>String Interpolation<a class="headerlink" href="#string-interpolation" title="Permalink to this headline">¶</a></h3>
<p>String interpolation (<code class="docutils literal"><span class="pre">#{expression}</span></code>) allows any valid expression to appear
within a <em>double-quoted string</em>. The result of evaluating that expression is
inserted into the string:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="s2">"foo #{bar} baz"</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{{</span> <span class="s2">"foo #{1 + 2} baz"</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="whitespace-control">
<span id="templates-whitespace-control"></span><h2>Whitespace Control<a class="headerlink" href="#whitespace-control" title="Permalink to this headline">¶</a></h2>
<p>The first newline after a template tag is removed automatically (like in PHP.)
Whitespace is not further modified by the template engine, so each whitespace
(spaces, tabs, newlines etc.) is returned unchanged.</p>
<p>Use the <code class="docutils literal"><span class="pre">spaceless</span></code> tag to remove whitespace <em>between HTML tags</em>:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">spaceless</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> <div></span>
<span class="x"> <strong>foo bar</strong></span>
<span class="x"> </div></span>
<span class="cp">{%</span> <span class="k">endspaceless</span> <span class="cp">%}</span><span class="x"></span>
<span class="c">{# output will be <div><strong>foo bar</strong></div> #}</span><span class="x"></span>
</pre></div>
</div>
<p>In addition to the spaceless tag you can also control whitespace on a per tag
level. By using the whitespace control modifier on your tags, you can trim
leading and or trailing whitespace:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">value</span> <span class="o">=</span> <span class="s1">'no spaces'</span> <span class="cp">%}</span><span class="x"></span>
<span class="c">{#- No leading/trailing whitespace -#}</span><span class="x"></span>
<span class="cp">{%</span>- <span class="k">if</span> <span class="kp">true</span> -<span class="cp">%}</span><span class="x"></span>
<span class="x"> </span><span class="cp">{{</span><span class="o">-</span> <span class="nv">value</span> -<span class="cp">}}</span><span class="x"></span>
<span class="cp">{%</span>- <span class="k">endif</span> -<span class="cp">%}</span><span class="x"></span>
<span class="c">{# output 'no spaces' #}</span><span class="x"></span>
</pre></div>
</div>
<p>The above sample shows the default whitespace control modifier, and how you can
use it to remove whitespace around tags. Trimming space will consume all whitespace
for that side of the tag. It is possible to use whitespace trimming on one side
of a tag:</p>
<div class="highlight-jinja"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">value</span> <span class="o">=</span> <span class="s1">'no spaces'</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"><li> </span><span class="cp">{{</span><span class="o">-</span> <span class="nv">value</span> <span class="cp">}}</span><span class="x"> </li></span>
<span class="c">{# outputs '<li>no spaces </li>' #}</span><span class="x"></span>
</pre></div>
</div>
</div>
<div class="section" id="extensions">
<h2>Extensions<a class="headerlink" href="#extensions" title="Permalink to this headline">¶</a></h2>
<p>Twig can be easily extended.</p>
<p>If you are looking for new tags, filters, or functions, have a look at the Twig official
<a class="reference external" href="http://github.com/twigphp/Twig-extensions">extension repository</a>.</p>
<p>If you want to create your own, read the <a class="reference internal" href="advanced.html#creating-extensions"><span class="std std-ref">Creating an
Extension</span></a> chapter.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Twig for Template Designers</a><ul>
<li><a class="reference internal" href="#synopsis">Synopsis</a></li>
<li><a class="reference internal" href="#ides-integration">IDEs Integration</a></li>
<li><a class="reference internal" href="#variables">Variables</a><ul>
<li><a class="reference internal" href="#global-variables">Global Variables</a></li>
<li><a class="reference internal" href="#setting-variables">Setting Variables</a></li>
</ul>
</li>
<li><a class="reference internal" href="#filters">Filters</a></li>
<li><a class="reference internal" href="#functions">Functions</a></li>
<li><a class="reference internal" href="#named-arguments">Named Arguments</a></li>
<li><a class="reference internal" href="#control-structure">Control Structure</a></li>
<li><a class="reference internal" href="#comments">Comments</a></li>
<li><a class="reference internal" href="#including-other-templates">Including other Templates</a></li>
<li><a class="reference internal" href="#template-inheritance">Template Inheritance</a></li>
<li><a class="reference internal" href="#html-escaping">HTML Escaping</a><ul>
<li><a class="reference internal" href="#working-with-manual-escaping">Working with Manual Escaping</a></li>
<li><a class="reference internal" href="#working-with-automatic-escaping">Working with Automatic Escaping</a></li>
</ul>
</li>
<li><a class="reference internal" href="#escaping">Escaping</a></li>
<li><a class="reference internal" href="#macros">Macros</a></li>
<li><a class="reference internal" href="#expressions">Expressions</a><ul>
<li><a class="reference internal" href="#literals">Literals</a></li>
<li><a class="reference internal" href="#math">Math</a></li>
<li><a class="reference internal" href="#logic">Logic</a></li>
<li><a class="reference internal" href="#comparisons">Comparisons</a></li>
<li><a class="reference internal" href="#containment-operator">Containment Operator</a></li>
<li><a class="reference internal" href="#test-operator">Test Operator</a></li>
<li><a class="reference internal" href="#other-operators">Other Operators</a></li>
<li><a class="reference internal" href="#string-interpolation">String Interpolation</a></li>
</ul>
</li>
<li><a class="reference internal" href="#whitespace-control">Whitespace Control</a></li>
<li><a class="reference internal" href="#extensions">Extensions</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="installation.html"
title="previous chapter">Installation</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="api.html"
title="next chapter">Twig for Developers</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/templates.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="api.html" title="Twig for Developers"
>next</a> |</li>
<li class="right" >
<a href="installation.html" title="Installation"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">php-twig-doc 2.4.6 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright by the Twig Team.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.7.
</div>
</body>
</html>
|