Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates. It's an open source product licensed under a BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher.
Despite being one the most full-featured PHP templating engines, Twig is also the fastest one:
Library | Time (sec) | Memory (Ko) | Templates rendered per second |
---|---|---|---|
Twig | 3 | 1,190 | 3,333 |
PHPTAL | 3.8 | 2,100 | 2,632 |
Dwoo | 6.9 | 1,870 | 1,449 |
Smarty 2 | 12.9 | 2,350 | 775 |
Smarty 3 | 14.9 | 3,230 | 671 |
Calypso | 34.3 | 620 | 292 |
eZ Templates | 53 | 5,850 | 189 |
Twig is a way to make your php more object and less messy and easy to read.
In this article, we'll share a collection of 10 tips and basics that every developer should know how to manipulate in Twig.
10. Variable declaration
You can assign values to variables inside code blocks. Assignments use the set tag:
Twig allow you to manipulate multiple values in variables. You can handle objects with JSON-Like notation.
You can use a dot (.) to access attributes of a variable if it has (methods or properties of a PHP object, or items of a PHP array), or the so-called "subscript" syntax ([index]
).
Note that if a property contains special characters (as dots or minus : myVariable.my-property
or myVariable.my.property
) you'll need to use the attribute function to access it as normally that syntax would cause a Twig error :
9. String interpolation
Just like PHP does with variables between double quotes :
Twig support this feature too and you can even execute inline operations with variable as concatenation and math :
Note: If you want to interpolate use always double quotes (""), if you use single quotes ('') the interpolation will be not used.
8. The loop variable inside a for loop and else tag
Inside of a {%for%}
loop block you can access the special variable loop which contains the following properties:
Variable | Description |
---|---|
loop.index |
The current iteration of the loop. (1 indexed) |
loop.index0 |
The current iteration of the loop. (0 indexed) |
loop.revindex |
The number of iterations from the end of the loop (1 indexed) |
loop.revindex0 |
The number of iterations from the end of the loop (0 indexed) |
loop.first |
True if first iteration |
loop.last |
True if last iteration |
loop.length |
The number of items in the sequence |
loop.parent |
The parent context |
For example, to check if an item is the last of the iteration, you can use:
Note: The loop.length, loop.revindex, loop.revindex0, and loop.last variables are only available for PHP arrays, or objects that implement the Countable interface. They are also not available when looping with a condition.
Although the {%for%}
tag is not an {%if%}
tag (you don't say), it supports the {%else%}
tag. It becomes automatically a condition if the variable to iterate is empty.
Although the else tag is useful, you can check if a variable is iterable using the is iterable statement:
Sometimes, when using nested loops, you need to access the parent context. The parent context is always accessible via the loop.parent
variable. For instance, if you have the following template data:
And the output should look like :
* 1: topic1 - 1.1: Message 1 of topic 1 - 1.2: Message 2 of topic 1 * 2: topic2 - 2.1: Message 1 of topic 2 - 2.2: Message 2 of topic 2
7. Extend a block without remove existing content
If you have an existing block which is located in other file (i.e : base.html.twig) and you need to append in a chield view (i.e child.html.twig) more things to an existent block (in this example more links), you can create a block with the same name in the child view and use the parent() function to retrieve the content of the original block (without the parent function, the block would be only replaced and the content of the parent template wouldn't exist).
If you don't still get it, read the following article to understand completely how parent works.
6. Twig supports Regular Expressions
Twig supports awesome string shortcuts for basic and frequently tasks with strings () and cool functions like starts and ends with :
However, not all in the life is starts or ends with. Sometimes there are more complex mechanisms of comparison that you'll need to implement according to your needs and you'll probably solve with Regex.
Twig supports Regular Expressions and you can use them using the matches
statement.
5. Control the spaces when you print variables
Twig normally include any space between tags, i.e :
However, you can prevent this behaviour easily. Twig allow you to clean unnecessary spaces between tags of 2 ways :
- control whitespace on a per tag level.
{% spaceless %}
tags.
Control whitespace on a per tag level, allow you to remove spaces from everything you print. To use it include a minus symbol after and before (respectively) of any print tag :
Note that, if you want you can use only one side instead of both :
While the spaceless tag allow you to print an HTML block without spaces (which provides a minifier effect) :
4. Pretty print a JSON encoded array with twig
Twig supports constants with the constant method.
In PHP you use to pretty print a json string using the JSON_PRETTY_PRINT constant as parameter to the json_encode function.
Twig uses json_encode under the hood, therefore you'll still need to use the constant:
3. Modify dates
With Twig you can modify dates easily as you usually did with DateTime objects in PHP.
The date_modify filter modifies a date with a given modifier string :
2. Limit string length
There are many cases where you want to truncate a string to prevent unwanted behaviours. To cut a string with a default length use the slice
filter.
You can ever use the ternary operator to limit the length of the text and put three dots :
1. Use macro
Macros are comparable with functions in regular programming languages. They are useful to put often used HTML idioms into reusable elements to not repeat yourself.
Basically and theoretically: is a type of self-written twig function which outputs custom html according to received parameters.
The macro's needs to be located in another twig file as a good practice and to include it in your current file use the import tag :
However, you can add macro's wherever you are. If you decide to add them in the same file where you are working you just need to the _self path :
We can use macro's to print repetitive html blocks as forms. In the following example we'll use a simple macro to print a bootstrap register form :
And the output should be similar to :
You can see a working macro in the following fiddle :
Frequently asked Twig questions
How to concatenate strings in Twig
In PHP we used to concatenate strings using the .
(dot) symbol. In Twig we'll use the ~
(tilde) symbol instead to concatenate strings.
You can use too the interpolation of strings as mentioned in the 9th point.
How to check if a variable exists
According to the configuration of your Twig environment, you may want to check if a variable exists to prevent errors.
To check if a variable exists use the is defined
statement.
How to check if a string or array contains a value
To check if an element is inside an array, you can use the in
statement.
Extra
You can test your Twig snippets online in twigfiddle.com which is quick, accessible and faster than create a Twig environment locally.
Have fun !
4 Comments