Remember, there are 'TWIG' tools and then there are 'Timber' tools.
The second is WordPress specific.
TWIG supports a rather verbose set of variables, but they are all either arrays or strings when it comes down to it.
See https://twig.symfony.com/doc/1.x/tags/set.html for more information.
Note: Variables are all scoped. If you need a variable outside of a loop (or subtemplate), declare it outside of the loop (or subtemplate).
{# Create a Basic Variable #}
{% set foo = 'bar' %}
{# Display the Basic Variable #}
{{ foo }}
There's some flexibility with variables, such as assigning arrays to them or even declaring more than one in a single block:
{% set foo = [1, 2] %}
{% set foo = {'foo': 'bar'} %}
{% set foo = 'foo' ~ 'bar' %}
One of the most useful (and abused by Timber) tools of set
is to capture chunks of text as a string:
{% set foo %}
<div id="pagination">
...
</div>
{% endset %}
When built correctly, the <template>.php
file will load:
$block
- The block settings and attributes from
$block
. - See https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/ for available attributes to load.
- Available Block Settings
- Useful
block.id
- Unique 'id' for the block (used in theid=""
wrapper).block.align
- The set 'alignment' for the block, can be configurable via ACF.block.supports
- Array of configurable field-settings.block.className
- The user-set 'Additional CSS classes'.
- Example Content
block.data
- Array of optional content. Requires usage ofexample
data-set. Otherwise is just the Administrative instructions for the block.
- Administrative
block.name
block.title
block.category
block.icon
block.keywords
block.post_types
- Available post types.block.enqueue_script
/block.enqueue_assets
- Defaults to 'false', if not set.
- Useful
- The block settings and attributes from
$fields
(Look for$content
)- The ACF fields from
get_fields()
. - (We at Taoti tend to also use
$content
instead.)
- The ACF fields from
$is_preview
- The Editor 'example' view of the block, for users to select from.
{{ block.id }}
{{ block.title }}
Filters are used to 'rewrite' the output of "content" on page load.
For example, the |date()
filter allows you to display a date, whereas the |towebp
filter changes an Image()
from its current format to a .webp
file.
Its important to note that there are both TWIG-specific and Timber-created filters, so you'll want to check both the Timber Documentation and the TWIG Documentation for details.
Its also important to remember that TWIG loads content ONE WAY. If it reaches the .twig
file, it will not be altered in the database.
-
WordPress 'Auto-paragraph' filter
<p class="content">{{ post.my_custom_text|wpautop }}</p>
-
Execute Shortcode
<p class="content">{{ post.my_custom_text|shortcodes }}</p>
- Note: See the upcoming 'Shortcodes' section for more usage of shortcodes.
-
Code Samples
<div class="code-sample">{{ post.code_samples|pretags }}</div>
-
Executing PHP
<p class="entry-meta">{{ function('twentytwelve_entry_meta') }}</p>
- See 'Functions' on the other Cheatsheet for more details.
-
Date Filters
- Note: There are various methods of writing the date.
<span class="date">{{ post.post_date|date('F j, Y') }}</span>
<p class="copyright">© {{ now|date('Y') }} by {{ bloginfo('name') }}</p>
<p class="entry-meta">Posted: <time>{{post.post_date_gmt|time_ago}}</time></p>
- If you need something stronger, see https://timber.github.io/docs/v2/guides/date-time/.
-
Image Filters
- Note: See the separate 'Image Cheatsheet' for more details.
-
Relative
- Convert an absolute URL into a relative one.
{{ 'http://example.org/2015/08/my-blog-post' | relative }}
-
Sanitize
- Converts spaces in titles to dashes.
- This is VERY useful for creating
id=
tags and such! {{post.title|sanitize}}
-
Excerpt
- Trim text to locked down length.
- Note: I would recommend be careful using this, as ACFs tend to allow character limits and it can be confusing at times.
<p class="intro">{{post.post_content|excerpt(30)}}...</p>
-
List
- Allows you to skip creating a 'loop' when displaying an array of items.
- Note the missing Oxford comma though.
- Template:
Contributions made by {{contributors|list(',','&')}}
- Output:
Contributions made by Item 1, Item 2 & Item 3