Relevant Timber Snippets

You find some relevant Timber snippets below:

Get CPT

$args = array (
    'post_type'              => array( 'mycpt' ),
    'post_status'            => array( 'publish' ),
    'order'                  => 'ASC',
    'orderby'                => 'menu_order',
);

// The Query
$query = new WP_Query( $args );

$context = Timber::get_context();
$context['mycpts'] = Timber::get_posts($query);
return Timber::compile('twig/my_template.twig', $context);

Rendering

Echo rendered output:

Timber::render('twig/my_template.twig', $context);

Return rendered output:

Timber::compile('twig/my_template.twig', $context);

Loop

example.twig:

<ul>
    {% for post in posts %}
        <li><a href="{{ post.permalink }}">{{ post.title }}</a></li>
    {% else %}
        <li>Sorry, no posts matched your criteria</li>
    {% endfor %}
</ul>

Use Variables

{% set foo = 'bar' %}

Get AdvancedCustomFields Value

Normal field:

{{post.get_field('my_wysiwyg_field')}}

Repeater field:

<h2>{{post.title}}</h2>
<div class="my-list">
    {% for item in post.get_field('my_repeater') %}
        <div class="item">
            <h4>{{item.name}}</h4>
            <h6>{{item.info}}</h6>
            <img src="{{TimberImage(item.picture).src}}" />
        </div>
    {% endfor %}
</div>

Debugging

Use the dump function to get debug informations for an object:

{{dump(post)}}