Being Cool With Twig Templating
So this is going to be an ongoing post, (when I remember to update it), about cool things in the Twig templating engine that you can do, but may not have known about. If you have some cool shortcuts or tricks, definitely leave them in the comments and I’ll add them to the list!
Simple Blocks
For simple blocks, like a ‘title’ block that may only contain a string you can turn this:
{% block title %} My Title Goes Here {% endblock title %}
into this:
{% block title 'My Title Goes Here' %}
Set Variables
Setting variables within twig is super easy. In the example below I’m setting the variable as a list of names. You probably wouldn’t do it like this in a real scenario, but you get the gist.
{% set names = ['Tiffany', 'Rider', 'Kade', 'Sean', 'Nick', 'Mike'] %}
Loop Variable
The loop
variable can come in especially handy and is available in a for
tag. Let’s say we wanted to loop our list of names and do something special on the first and last iteration. We could use the loop.first and loop.last variables.
{% for name in names %} {% if loop.first %} // Do something on first iteration {% endif %} {% if loop.last %} // Do something on last iteration {% endif %} {% endfor %}
There are plenty more where that came from. Check out more loop variables in the official docs.
Condition In Your For Loop
Another cool feature you can take advantage of in a loop is a condition which will filter what gets output. Let’s look at the names array again as an example.
<ul> {% for name in names if name|length > 5 %} <li>{{ name }}</li> {% endfor %} </ul>
The above code will only display “Tiffany” on output.
Printing Out Blocks Multiple Times
If you are using a template that inherits (extends) another template, you can print out a block of content multiple times by just using the block function.
{% block user %}{% endblock user %} <div> {{ block('user') }} </div>
Removing Whitespace
Their are numerous ways to remove white space from the output. The first and maybe the most commonly seen is the spaceless
tag.
{% spaceless %} <div> <p>Here is some text.</p> </div> {% endspaceless %} renders: <div><p>Here is some text.</p></div>
Another, and maybe less known way to remove whitespace from output is using a “-” on an individual tag. This allows you to trim leading or trailing whitespace.
{% set var = 'Here is some text.' %} {%- if true -%} <p> {{- var -}} </p> {%- endif -%} renders: <p>Here is some text.</p>
You can also just trim white space on one side or the other if necessary.
{% set var = 'Here is some text.' %} <p> {{ var -}} </p> renders: <p> Here is some text.</p>
Twig Tests
Twig is pretty awesome at logic tests. For example if you want to check if one variable is exactly equal to another variable, (===), you can use the is same as(variable)
test.
{% if entityType is same as('Account') %} The entity is an Account! {% endif %}
Display Class Constants
Displaying a class’s constant value in a Twig file is simply achieved with the constant()
function. Let’s say that the RECORDS_TO_SHOW
constant has a value of 10.
<p>There are {{ constant(RECORDS_TO_SHOW, entity) }} per page.</p> renders: <p>There are 10 records per page.</p>
No Comments