Twig
Last updated
Was this helpful?
Last updated
Was this helpful?
The Advanced Views templates by default, utilize the, a modern and widely adopted template engine.
If you're new to Twig, we provide an overview of its key features below.
You can launch the snippets and play with them in your browser using the service.
Using double curly brackets, you can easily inject variables into your markup. For instance:
When working with arrays, follow this syntax:
With Twig, you don't need to worry about escaping variables anymore because Twig automatically escapes all variables passed into the template by default.
However, in some specific cases, you may still need to pass HTML without escaping. For example, when displaying a WYSIWYG field value, you should use the |raw
filter like this: {{ my_var|raw }}
. Without the |raw
filter, HTML tags will be converted into HTML entities and displayed on the page as plain text.
Twig allows you to perform any mathematical operations within brackets. For example:
This will output 15.
The 'else' part is optional.
Additionally, you can access the current index using the loop.index
magic variable, which starts at "1". This can be useful for detecting the first element or distinguishing between odd and even indices.
Here are some examples:
Functions, unlike filters, serve different purposes and have distinct syntax.
The key distinction between filters and functions lies in how they handle variables. Filters modify the variable they are applied to, as in "var|round," while functions operate on arguments independently.
When working with date filters and functions, it's important to consider the variable type. Strings containing dates and Date objects are distinct entities, and you should keep the following in mind:
Date objects cannot be displayed directly; you need to convert them into strings for this purpose.
When using conditional statements like {% if %}
, you cannot directly compare date strings; you should first convert them into Date objects for comparison.
The "date" filter returns a string, whereas the "date_modify" filter and "date" function return Date objects. Refer to the information below for more details.
Keep in mind that this function returns a Date object, not a string. If you want to display the modified date, you should use the "date" filter to print the Date object in a specific format.
Converts a string into a Date object, which is useful for comparison purposes.
Keep in mind that this function also returns a Date object, not a string. If you want to display the Date object, you should use the "date" filter to print it in a specific format.
Available date formats
you can use any formats with the |date filter
you can use most of the available date formats for the date() function. For example, "m-d-Y" (10-20-2011), "M d, Y" (October 20, 2011), or "d.m.Y" (20.10.2011).
We extended Twig to include additional WordPress-related functions, in addition to its built-in functions. You can find these extended functions listed below.
If you're wondering, Advanced Views uses version 3.7.1 of the Twig engine.
You can also directly within a template:
You can create different markup based on . For instance:
You can also use and for comparison, such as:
Twig provides a straightforward way to :
Twig offers a wide that you can apply using the pipe symbol.
Click on a specific filter name on in the Twig Docs to access more detailed information.
Note: We have created a custom build of Twig (to securely integrate it with WordPress), so some filters from the may be missing. You can safely use the filters listed above or experiment with any filter from the and let us know if it doesn't function as expected.
Converts a Date object into a string based on the specified format. If you provide a string, the filter will automatically parse the date using and then convert it into a string in the requested format.
Modifies a Date object according to the argument provided and returns the modified Date object. If you pass a string, the filter will automatically parse the date using .
Tip: When you use the date parsing functions and filters, we recommend using the '.timestamp
' property of the date field. This will save you from dealing with date parsing issues.
For example, both US (e.g., mm/dd/yyyy) and European (e.g., dd/mm/yyyy) formats use the same delimiter, so the European format will be ignored in favor of the US format. This behavior is in line with the default behavior of the function.
You'll use the interactivity functions in Views when employing the , while paginate_links
is used inside Cards when the 'Source: Page content' option is chosen.
In the Pro version of the plugin, you can add your own functions. Check to learn details.
In the Pro version of the plugin, you can add your own filters. Check to learn details.