Twig Documentation Release Stable
Total Page:16
File Type:pdf, Size:1020Kb
Twig Documentation Release stable Dec 16, 2018 Contents 1 Introduction 1 1.1 Prerequisites...............................................1 1.2 Installation................................................1 1.3 Basic API Usage.............................................2 2 Installation 3 3 Twig for Template Designers5 3.1 Synopsis.................................................5 3.2 IDEs Integration.............................................6 3.3 Variables.................................................6 3.4 Filters...................................................7 3.5 Functions.................................................8 3.6 Named Arguments............................................8 3.7 Control Structure.............................................9 3.8 Comments................................................9 3.9 Including other Templates........................................9 3.10 Template Inheritance........................................... 10 3.11 HTML Escaping............................................. 11 3.12 Escaping................................................. 12 3.13 Macros.................................................. 12 3.14 Expressions................................................ 13 3.15 Whitespace Control........................................... 17 3.16 Extensions................................................ 17 4 Twig for Developers 19 4.1 Basics................................................... 19 4.2 Rendering Templates........................................... 20 4.3 Environment Options........................................... 20 4.4 Loaders.................................................. 21 4.5 Using Extensions............................................. 24 4.6 Built-in Extensions............................................ 24 4.7 Exceptions................................................ 28 5 Extending Twig 29 5.1 Globals.................................................. 30 5.2 Filters................................................... 31 i 5.3 Functions................................................. 33 5.4 Tests................................................... 33 5.5 Tags.................................................... 35 5.6 Creating an Extension.......................................... 37 5.7 Overloading............................................... 42 5.8 Testing an Extension........................................... 43 6 Twig Internals 45 6.1 How does Twig work?.......................................... 45 6.2 The Lexer................................................. 45 6.3 The Parser................................................ 46 6.4 The Compiler............................................... 47 7 Recipes 49 7.1 Displaying Deprecation Notices..................................... 49 7.2 Making a Layout conditional....................................... 50 7.3 Making an Include dynamic....................................... 50 7.4 Overriding a Template that also extends itself.............................. 50 7.5 Customizing the Syntax......................................... 51 7.6 Using dynamic Object Properties.................................... 52 7.7 Accessing the parent Context in Nested Loops............................. 52 7.8 Defining undefined Functions and Filters on the Fly.......................... 53 7.9 Validating the Template Syntax..................................... 54 7.10 Refreshing modified Templates when OPcache or APC is enabled................... 54 7.11 Reusing a stateful Node Visitor..................................... 54 7.12 Using a Database to store Templates................................... 55 7.13 Using different Template Sources.................................... 56 7.14 Loading a Template from a String.................................... 57 7.15 Using Twig and AngularJS in the same Templates........................... 57 8 Coding Standards 59 9 Tags 61 9.1 autoescape .............................................. 61 9.2 block .................................................. 62 9.3 do .................................................... 62 9.4 embed .................................................. 62 9.5 extends ................................................ 65 9.6 filter ................................................. 69 9.7 flush .................................................. 69 9.8 for .................................................... 69 9.9 from ................................................... 71 9.10 if .................................................... 72 9.11 import ................................................. 73 9.12 include ................................................ 74 9.13 macro .................................................. 75 9.14 sandbox ................................................ 76 9.15 set .................................................... 77 9.16 spaceless ............................................... 78 9.17 use .................................................... 78 9.18 verbatim ................................................ 80 9.19 with ................................................... 80 9.20 deprecated .............................................. 81 10 Filters 83 ii 10.1 abs .................................................... 83 10.2 batch .................................................. 83 10.3 capitalize .............................................. 84 10.4 convert_encoding ......................................... 84 10.5 date ................................................... 85 10.6 date_modify ............................................. 86 10.7 default ................................................ 86 10.8 escape ................................................. 86 10.9 first .................................................. 88 10.10 format ................................................. 88 10.11 join ................................................... 88 10.12 json_encode ............................................. 89 10.13 keys ................................................... 89 10.14 last ................................................... 89 10.15 length ................................................. 90 10.16 lower .................................................. 90 10.17 merge .................................................. 90 10.18 nl2br .................................................. 91 10.19 number_format ............................................ 91 10.20 raw .................................................... 92 10.21 replace ................................................ 92 10.22 reverse ................................................ 93 10.23 round .................................................. 94 10.24 slice .................................................. 94 10.25 sort ................................................... 95 10.26 split .................................................. 95 10.27 striptags ............................................... 96 10.28 title .................................................. 97 10.29 trim ................................................... 97 10.30 upper .................................................. 97 10.31 url_encode .............................................. 98 11 Functions 99 11.1 attribute ............................................... 99 11.2 block .................................................. 99 11.3 constant ................................................ 100 11.4 cycle .................................................. 100 11.5 date ................................................... 101 11.6 dump ................................................... 101 11.7 include ................................................ 102 11.8 max .................................................... 103 11.9 min .................................................... 103 11.10 parent ................................................. 104 11.11 random ................................................. 104 11.12 range .................................................. 104 11.13 source ................................................. 105 11.14 template_from_string ...................................... 106 12 Tests 107 12.1 constant ................................................ 107 12.2 defined ................................................ 107 12.3 divisible by ............................................ 108 12.4 empty .................................................. 108 12.5 even ................................................... 108 iii 12.6 iterable ................................................ 108 12.7 null ................................................... 109 12.8 odd .................................................... 109 12.9 same as ................................................ 109 iv CHAPTER 1 Introduction This is the documentation for Twig, the flexible, fast, and secure template engine for PHP. If you have any exposure to other text-based template languages, such as Smarty, Django, or Jinja, you should feel right at home with Twig. It’s both designer and developer friendly by sticking to PHP’s principles and adding functionality useful for templating environments. The key-features are. • Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum. • Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design. • Flexible: