<p>I'm not sure if this is better or not but one thing I'd do here is to set some defaults in my include. This also means you don't need to worry about checking whether it's defined or not. You're just checking whether it's true.</p>

<pre><code>{% set options = {
    author: true,
    badge: true,
    date: true,
    magnify: true,
    meta: true,
    thumbnail: true,
} | merge(cardOptions | default({})) %}

&lt;div class="card"&gt;

    {% if options.badge %}
        &lt;div class="badge"&gt;
            &lt;img src="/assets/images/icon-new.svg" height="14px" width="14px" alt="New!"&gt;
        &lt;/div&gt;
    {% endif %}

    {% if options.thumbnail %}
        &lt;a href="#"&gt;
            &lt;img src="http://placehold.it/320x210" 
                srcset="http://placehold.it/640x420 750w, 
                     http://placehold.it/320x210 280w" 
                sizes="100%"
                alt=""
                class="tn"&gt;
        &lt;/a&gt;
    {% endif %}

    &lt;article&gt;
        &lt;h2&gt;&lt;a href="#"&gt;Heading Goes in Here&lt;/a&gt;&lt;/h2&gt;

        {% if options.meta %}

            &lt;footer class="meta"&gt;
                {% if options.date %}
                    {% include "_modules/relative-date" %} 
                {% endif %}
                {% if options.author %}
                    by &lt;a href="#" rel="author"&gt;John Doe&lt;/a&gt;
                {% endif %}
            &lt;/footer&gt;

        {% endif %}

    &lt;/article&gt;
&lt;/div&gt;
</code></pre>

<p>Then when I use this include I can pass in an object of options that could overide those defaults. But I only need to pass in values that need to change the defaults. For example here I can set <code>meta</code> to <code>false</code> but can leave all the others to be handled by the default:</p>

<pre><code>{% set cardOptions = {
    meta: false
} %}

{% include "_modules/card" with cardOptions %}
</code></pre>

<p>You said this would be included within an <code>entries</code> loop. You could do this in that case:</p>

<pre><code>{% set options = {
    entry: false,
    author: true,
    badge: true,
    date: true,
    magnify: true,
    meta: true,
    thumbnail: true,
} | merge(cardOptions | default({})) %}

{# Wrap the whole card in a conditional #}
{% if options.entry %}
   // Card goes here
{% endif %}
</code></pre>

<p>And then in your <code>entries</code> loop you'd do:</p>

<pre><code>{% for entry in entries %}

    {% set cardOptions = {
        entry: entry
    } %}

    {% include "_modules/card" with cardOptions only %}
{% endfor %}
</code></pre>

<p>I'd probably use the <code>only</code> option on the <code>include</code> tag to avoid stuff leaking in.</p>

<p>I've also played with options such as:</p>

<pre><code>{% set cardOptions = {
    styleGuide: true
} %}
</code></pre>

<p>In my include this option will have a <code>false</code> default I'll have a conditional that checks for it being <code>true</code> and if so it'll ignore any dynamic content and instead I'll output an example of the module with fake content. It's good for testing and for keeping a library of modules.</p>

<p>You could also consider a macro that wraps your <code>include</code> tag. I gave a similar example in a blog post not long ago: <a href="http://withchief.com/blog/using-macros-to-improve-consistency-in-your-craft-cms-templates" rel="nofollow">Using macros to improve consistency in your Craft CMS templates</a></p>

<pre><code>{% macro module(type, options = {}) %}
    {% include '_modules/' ~ type with options only %}
{% endmacro %}
</code></pre>

<p>And then in your template you'd do something like:</p>

<pre><code>{% set cardOptions = {
    entry: entry,
    meta: false
} %}

{{ module('card', cardOptions) }}
</code></pre>

<p>Dunno if any of that helps!</p>

CC BY-SA 3.0
2016-04-07T08:17:10.977Z
Thanks Jamie, that's really helpful! I'll check out your blog post too – I've dabbled with macros before but I know I need to get properly stuck into them.
Ian Ebden
CC BY-SA 3.0
2016-04-06T10:42:30.873Z
2016-04-06T10:52:53.167Z
2016-04-06T10:52:53.167Z
Jamie Pittock
<p>I'm sure there must be a tidier and more efficient way of doing this so thought I'd throw it out there for some opinions.</p>

<p>Building a site that has the usual lists (in grid format) for Blog, Gallery, Meet the Team, etc... I'm calling these 'cards', and I'm including them with various attributes:</p>

<pre><code>{% include "_modules/card"
    with {
        author: true,
        badge: true,
        date: true,
        magnify: true,
        meta: true,
        thumbnail: true,
    }
%}
</code></pre>

<p>Then in my card I've got:</p>

<pre><code>&lt;div class="card"&gt;

{% if badge is defined %}
    &lt;div class="badge"&gt;
        &lt;img src="/assets/images/icon-new.svg" height="14px" width="14px" alt="New!"&gt;
    &lt;/div&gt;
{% endif %}

{% if thumbnail is defined %}
    &lt;a href="#"&gt;
        &lt;img src="http://placehold.it/320x210" 
             srcset="http://placehold.it/640x420 750w, 
                     http://placehold.it/320x210 280w" 
             sizes="100%"
             alt=""
             class="tn"&gt;
    &lt;/a&gt;
{% endif %}

&lt;article&gt;
    &lt;h2&gt;&lt;a href="#"&gt;Heading Goes in Here&lt;/a&gt;&lt;/h2&gt;

    {% if meta is defined %}

        &lt;footer class="meta"&gt;
            {% if date is defined %}
                {% include "_modules/relative-date" %} 
            {% endif %}
            {% if author is defined %}
                by &lt;a href="#" rel="author"&gt;John Doe&lt;/a&gt;
            {% endif %}
        &lt;/footer&gt;

    {% endif %}

&lt;/article&gt;&lt;/div&gt;
</code></pre>

<p>This all works as expected but I can't help feel it could be neater. There are a lot of conditions at work here for example, and this will all be part of an entries loop too. Anyone have any suggestions for streamlining this a bit? Much appreciated.</p>

<conditional><forloop><include>
Streamlining a loop with a bunch of conditions
# Streamlining a loop with a bunch of conditions

(Asked by: username_0 on 2016-04-06)

I'm sure there must be a tidier and more efficient way of doing this so thought I'd throw it out there for some opinions.
Building a site that has the usual lists (in grid format) for Blog, Gallery, Meet the Team, etc... I'm calling these 'cards', and I'm including them with various attributes:
{% include "_modules/card"
    with {
        author: true,
        badge: true,
        date: true,
        magnify: true,
        meta: true,
        thumbnail: true,
    }
%}

Then in my card I've got:


{% if badge is defined %}
    
        
    
{% endif %}

{% if thumbnail is defined %}
    
        
    
{% endif %}


    Heading Goes in Here

    {% if meta is defined %}

        
            {% if date is defined %}
                {% include "_modules/relative-date" %} 
            {% endif %}
            {% if author is defined %}
                by John Doe
            {% endif %}
        

    {% endif %}



This all works as expected but I can't help feel it could be neater. There are a lot of conditions at work here for example, and this will all be part of an entries loop too. Anyone have any suggestions for streamlining this a bit? Much appreciated.

**Tags:** conditional, forloop, include

---

## 1 answers

### Answer by username_1 on 2016-04-06. Score: 3 | Accepted answer

I'm not sure if this is better or not but one thing I'd do here is to set some defaults in my include. This also means you don't need to worry about checking whether it's defined or not. You're just checking whether it's true.
{% set options = {
    author: true,
    badge: true,
    date: true,
    magnify: true,
    meta: true,
    thumbnail: true,
} | merge(cardOptions | default({})) %}



    {% if options.badge %}
        
            
        
    {% endif %}

    {% if options.thumbnail %}
        
            
        
    {% endif %}

    
        Heading Goes in Here

        {% if options.meta %}

            
                {% if options.date %}
                    {% include "_modules/relative-date" %} 
                {% endif %}
                {% if options.author %}
                    by John Doe
                {% endif %}
            

        {% endif %}

    


Then when I use this include I can pass in an object of options that could overide those defaults. But I only need to pass in values that need to change the defaults. For example here I can set meta to false but can leave all the others to be handled by the default:
{% set cardOptions = {
    meta: false
} %}

{% include "_modules/card" with cardOptions %}

You said this would be included within an entries loop. You could do this in that case:
{% set options = {
    entry: false,
    author: true,
    badge: true,
    date: true,
    magnify: true,
    meta: true,
    thumbnail: true,
} | merge(cardOptions | default({})) %}

{# Wrap the whole card in a conditional #}
{% if options.entry %}
   // Card goes here
{% endif %}

And then in your entries loop you'd do:
{% for entry in entries %}

    {% set cardOptions = {
        entry: entry
    } %}

    {% include "_modules/card" with cardOptions only %}
{% endfor %}

I'd probably use the only option on the include tag to avoid stuff leaking in.
I've also played with options such as:
{% set cardOptions = {
    styleGuide: true
} %}

In my include this option will have a false default I'll have a conditional that checks for it being true and if so it'll ignore any dynamic content and instead I'll output an example of the module with fake content. It's good for testing and for keeping a library of modules.
You could also consider a macro that wraps your include tag. I gave a similar example in a blog post not long ago: Using macros to improve consistency in your Craft CMS templates
{% macro module(type, options = {}) %}
    {% include '_modules/' ~ type with options only %}
{% endmacro %}

And then in your template you'd do something like:
{% set cardOptions = {
    entry: entry,
    meta: false
} %}

{{ module('card', cardOptions) }}

Dunno if any of that helps!

> **Comments:**
> **username_0**: Thanks Jamie, that's really helpful! I'll check out your blog post too – I've dabbled with macros before but I know I need to get properly stuck into them.  

---

