Skip to content

Timber Twig Reference

Nicholas Davidson edited this page Dec 17, 2015 · 1 revision

Creating your own function filters

In your functions.php, inside your LaunchframeSite class, add the function

function add_to_twig($twig) {
    /* this is where you can add custom functions to your theme */
    $twig->addExtension(new Twig_Extension_StringLoader());
    $twig->addFilter('imgix', new Twig_Filter_Function('get_imgix'));
    return $twig;
}

For IMGIX specifically, in your twig file, then add this:

function get_imgix($arg_array = array()) {
    $domain = 'http://catalystnutra.imgix.net';
    $acf_image = $arg_array[0];
    $params = $arg_array[1];
    $acf_img_url = parse_url($acf_image);
    $url = $domain . $acf_img_url['path'] . '?' . $params;
    return $url;
}

In your twig files, you can pass your parameters like this, for instance:

{% set hero_image, hero_args = post.get_field('hero_bg_image'), 'w=1400&q=20&fm=jpg' %}
{% set imgix_args = [hero_image, hero_args] %}
<div class="overlay overlay-img" style="background-image: url('{{ imgix_args | imgix }}');">

Clone this wiki locally