Twig Cookbook

Filters

Generate handle from string ( use Wordpress sanitize_title function )

{{ 'My title'|handle }}

--> output : my-title

Handle Wordpress "Read more" tag

{{ post.content|more|raw }}

Resize image

<img src ="{{ post.thumbnail|resize(800,600) }}"/>

Generate picture

{{ post.thumbnail|picture(1280, 680, {'max-width: 420px':[420,665], 'max-width: 768px':[768,820]})|raw }}

--> ouput 

<picture>
    <source media="(max-width: 420px)" srcset="/uploads/image-420x665.webp" type="image/webp"/>
    <source media="(max-width: 420px)" srcset="/uploads/image-420x665.jpg" type="image/jpeg"/>
    <source media="(max-width: 768px)" srcset="/uploads/image-768x820.webp" type="image/webp"/>
    <source media="(max-width: 768px)" srcset="/uploads/image-768x820.jpg" type="image/jpeg"/>
    <source srcset="/uploads/image-1280x680.webp" type="image/webp"/>
    <img src="/uploads/image-1280x680.jpg" alt="" loading="lazy" width="1280" height="680"/>
</picture>

Functions

Execute php functions

{{ fn('sanitize_title', 'My title') }} 
{{ function('sanitize_title', 'My title') }}

Search content for shortcodes and filter shortcodes through their hooks

{{ shortcode(post.content) }}

Get login url

{{ login_url() }}

Display search form

{{ search_form() }}

Retrieves the permalink for a post type archive

{{ archive_url('guide') }}

Retrieve the URL for an attachment

{{ attachment_url(10) }}

Get post permalink by value, available options are : id, state, title

{# post_url(value, type) #}
{{ post_url('My post', 'title') }}

Get term permalink

{{ term_url(10, 'item') }}

Display dynamic sidebar

{{ dynamic_sidebar(1) }}

Outputs a complete commenting form for use within a template

{{ comment_form() }}

Determines whether a sidebar contains widgets

{{ is_active_sidebar(1) }}

Retrieve the translation of text

{{ _e('Submit') }}
{{ __('Submit') }}

Retrieve translated string with gettext context

{{ _x() }}

Translates and retrieves the singular or plural form based on the supplied number

{{ _n() }}

Fire the wp_head action

{{ wp_head() }}

Fire the wp_footer action.

{{ wp_footer() }}

Instantiate Wordpress bundle post entity

{% set post = Post(10) %}

Instantiate bundle user entity

{% set user = User(10) %}

Instantiate bundle term entity

{% set term = Term(10) %}

Instantiate bundle image entity

{% set image = Image(10) %}

Generate transparent image placeholder

<img src="{{ pixel(10,20) }}"/>

Last updated