<p>You're close! The way you have your conditional set up now will print Hero Image Markup if either of your conditionals fail. By checking if <code>block.displayHeroVideo</code> is 1 on a block type that doesn't have that field, Twig will still return 0. You should only check <code>displayHeroVideo</code> when you know you are inside of a <code>heroVideo</code> block. </p>

<pre><code>{% for entry in entry.heroFeatured.limit(1) %}
    {% for block in entry.hero %}

        {% if block.type == "heroVideo" %}

            {% if block.displayHeroVideo == "1" %} 

                &lt;h1&gt;Hero Video Markup&lt;/h1&gt;

            {% endif %}

        {% else %}

            &lt;h1&gt;Hero Image Markup&lt;/h1&gt;

        {% endif %}
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-07-22T15:17:52.310Z
This still prints both the heroVideo block and the heroImage block if the lightswitch in heroVideo is active. I'm trying to get it so that if lightswitch is active only display the heroVideo block.
Niall Thompson
CC BY-SA 3.0
2015-07-22T15:21:10.990Z
Will the heroVideo block always be the first block?
Aaron Berkowitz
CC BY-SA 3.0
2015-07-22T21:00:39.467Z
Here's my matrix field setup: http://prntscr.com/7vsvtq
Niall Thompson
CC BY-SA 3.0
2015-07-22T14:18:31.677Z
2017-07-31T18:38:20.177Z
2017-07-31T18:38:20.177Z
Aaron Berkowitz
<p>Might be better to just adjust the matrix field itself (upload a video or an image and make an if statement depending on file type).</p>

<p>It also is easier if you wouldnt put the image field inside the "hero" block. This way you won't have the issue regarding turning off the other block. </p>

<p>I would adjust your matrix to one of the mentioned methods above. If you do that you can easily access the light switch and make a if statement like:</p>

<pre><code>  {% for block in entry.hero %}

        {% if block.type == "heroBlock" %}

            {% if block.displayHeroVideo %}
                &lt;h1&gt;Hero Video Markup&lt;/h1&gt;
            {% else %}
                 &lt;h1&gt;Hero Image Markup&lt;/h1&gt;
            {% endif %}

        {% endif %}

  {% endfor %}
</code></pre>

<p>This works if you merge the 2 blocks into one (named Hero Block here). It will save you alot of time writing complicated code and also keeps your code clean.</p>

CC BY-SA 3.0
2017-07-27T14:37:20.820Z
2017-07-27T14:37:20.820Z
Justin Dekkers
<p>I have a section that needs to display a certain block type (heroVideo) if a lightswitch (displayHeroVideo) within it is active. Otherwise, it should display a different block type (heroImage).</p>

<p>How can I write a conditional to accommodate this?</p>

<p>Here's what I have already which outputs both blocks if the lightswitch is active. How do I apply the conditional earlier?</p>

<pre><code>{# Start hero section #}
{% for entry in entry.heroFeatured.limit(1) %}
  {% for block in entry.hero %}

  {% if block.type == "heroVideo" and block.displayHeroVideo == "1" %}

  &lt;h1&gt;Hero Video Markup&lt;/h1&gt;

  {% else %}

  &lt;h1&gt;Hero Image Markup&lt;/h1&gt;

  {% endif %}
{% endfor %}
{% endfor %}
</code></pre>

<matrix><conditional>
If Conditional for Lightswitch in Matrix Field
# If Conditional for Lightswitch in Matrix Field

(Asked by: username_0 on 2015-07-21)

I have a section that needs to display a certain block type (heroVideo) if a lightswitch (displayHeroVideo) within it is active. Otherwise, it should display a different block type (heroImage).
How can I write a conditional to accommodate this?
Here's what I have already which outputs both blocks if the lightswitch is active. How do I apply the conditional earlier?
{# Start hero section #}
{% for entry in entry.heroFeatured.limit(1) %}
  {% for block in entry.hero %}

  {% if block.type == "heroVideo" and block.displayHeroVideo == "1" %}

  Hero Video Markup

  {% else %}

  Hero Image Markup

  {% endif %}
{% endfor %}
{% endfor %}

**Tags:** matrix, conditional

---

## 2 answers

### Answer by username_1 on 2015-07-22. Score: 6

You're close! The way you have your conditional set up now will print Hero Image Markup if either of your conditionals fail. By checking if block.displayHeroVideo is 1 on a block type that doesn't have that field, Twig will still return 0. You should only check displayHeroVideo when you know you are inside of a heroVideo block. 
{% for entry in entry.heroFeatured.limit(1) %}
    {% for block in entry.hero %}

        {% if block.type == "heroVideo" %}

            {% if block.displayHeroVideo == "1" %} 

                Hero Video Markup

            {% endif %}

        {% else %}

            Hero Image Markup

        {% endif %}
    {% endfor %}
{% endfor %}

> **Comments:**
> **username_0**: This still prints both the heroVideo block and the heroImage block if the lightswitch in heroVideo is active. I'm trying to get it so that if lightswitch is active only display the heroVideo block.  
> **username_1**: Will the heroVideo block always be the first block?  
> **username_0**: Here's my matrix field setup: http://prntscr.com/7vsvtq  

---

### Answer by username_2 on 2017-07-27. Score: 1

Might be better to just adjust the matrix field itself (upload a video or an image and make an if statement depending on file type).
It also is easier if you wouldnt put the image field inside the "hero" block. This way you won't have the issue regarding turning off the other block. 
I would adjust your matrix to one of the mentioned methods above. If you do that you can easily access the light switch and make a if statement like:
  {% for block in entry.hero %}

        {% if block.type == "heroBlock" %}

            {% if block.displayHeroVideo %}
                Hero Video Markup
            {% else %}
                 Hero Image Markup
            {% endif %}

        {% endif %}

  {% endfor %}

This works if you merge the 2 blocks into one (named Hero Block here). It will save you alot of time writing complicated code and also keeps your code clean.


---

