How to filter a boolean in multiple contexts

Im using Timber to build my WordPress websites. It brings a rich template engine to Wordpess, based on Twig.

One situation im not being to solve is the following.
I have a template file responsible to show posts in every loop context (archives, search, etc). This template is also used by a plugin I’ve made to showcase featured posts.

Read More

That plugin has a few options to show or hide pieces of information such as the thumbnail or the post excerpt.

The code for this is as follows.

Featured Posts plugin loop, which sets a few variables including those should later control if the thumbnail and excerpt should be visible:

   <?php while( $flexible_posts->have_posts() ) : $flexible_posts->the_post(); global $post; ?>

            <?php

                $data = Timber::get_context();
                $data['showthumb'] = $thumbnail ?: true;
                $data['thumbsize'] = $thumbsize ?: 'thumbnail';
                $data['excerpt']   = $excerpt;
                $data['post'] = new TimberPost( $post );

                if ( $post->post_type == 'product') {

                    Timber::render('woo/tease-product.twig', $data);

                } else {

                    Timber::render('partials/tease-post.twig', $data);

                }

            ?>

    <?php endwhile; ?>

The template loaded from the code above, triggered by the plugin but also from common wordpress loops to show the post “tease”:

<article {{ fn('post_class', ['$classes', 'entry'] ) }}>

    <div class="Media">

        {% do action('tha_entry_top') %}

        <div class="Media-figure">
            {% if post.thumbnail %}
                <img src="{{ post.thumbnail.src('thumbnail') }}" />
            {% else %}
                <span class="thumb-placeholder"><i class="icon-camera"></i></span>
            {% endif %}
        </div>


        <div class="Media-content">
            <h2 class="entry-title"><a href="{{post.link}}">{{post.title}}</a></h2>
            <div class="entry-meta">{{post.date|date("F jS\, Y")}} in {{ post.terms('categories')|first }}</div>
            <p>{{post.get_preview(35)}}</p>
        </div>

        {% do action('tha_entry_bottom') %}

    </div>

</article>

My problem is, how can I use the plugin options so that it shows or hides the thumbnail image but still be able to use the same post template to output regular WP loops that do not have that variable set?

Related posts

Leave a Reply