<p>Using switch is a good way to handle this, but it looks like your conditionals are just a little off:</p>

<pre><code>{% for block in entry.contentBlock %}
    {% if block.type == 'text' %}
        {{ block.text }}
    {% elseif block.type == 'photo' %}
        {% set photo = block.photo.first() %}
        {% if photo %}
            &lt;img src="{{ photo.getUrl('thumb') }}" alt="{{ photo.title }}"&gt;
        {% endif %}
    {% endif %}
{% endfor %}
</code></pre>

<p>The same thing with the switch syntax would be:</p>

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

    {% switch block.type %}
        {% case "text" %}
            {{ block.text }}
        {% case "photo" %}
            {% set photo = block.photo.first() %}
            {% if photo %}
                &lt;img src="{{ photo.getUrl('thumb') }}" alt="{{ photo.title }}"&gt;
            {% endif %}
    {% endswitch %}

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

CC BY-SA 3.0
2015-04-28T21:31:53.403Z
I tried both of these methods and was not able to get the image/s to show using either; the text does show and seems to be the only part working. I tried amending the code for image.gerUrl to remove thumb, in case that that the issue, but that didn't work. Do I need to make further reference to where the image is located, or use a for entry in statement? Is these something missing which is assumed the user would have already included?
Craftnoob
CC BY-SA 3.0
2015-04-28T21:39:34.383Z
Is your second block named `photo` rather than `image`? If so try `case "photo"` for the second switch case. If the field in that block type is also named `photo` and not `image`, you'll need to update `block.image` to be `block.photo`.
Stephen Bowling
CC BY-SA 3.0
2015-04-28T22:59:13.053Z
The solution was to rename image to photo in both instances you mentioned. Thank you for helping.
Craftnoob
CC BY-SA 3.0
2015-04-29T01:23:59.667Z
You're welcome. I updated the code in my answer to use `photo` instead of `image`.
Stephen Bowling
CC BY-SA 3.0
2015-04-27T22:17:18.373Z
2015-04-29T01:23:18.507Z
2015-04-29T01:23:18.507Z
Stephen Bowling
<p>I have 1 Matrix field:
contentBlock</p>

<p>With 2 Block types:
text
photo</p>

<p>With nested fields:
text -> text
photo -> photo</p>

<p>My attempt at using if statements, which didn't work, to toggle between text and photo looks like the following:</p>

<pre><code>{% for block in entry.contentBlock %}
    {% if block.type == 'text' %}
        {{ block.text }}
    {% endif %}
    {% elseif block.type == 'image' %}
        {% set image = block.image.first() %}
            {% if image %}
                &lt;img src="{{ image.getUrl('thumb') }}" alt="{{ image.title }}"&gt;
            {% endif %}
{% endfor %}
</code></pre>

<p>Would using {% switch %} be a better method, or was I close to correct using the if statements?
I think getting the hang of this will open up the true posibilities of using Craft Matrix fields.</p>

<p>Thank you for any input on this question.</p>

<matrix><switch>
How do you use a Matrix to toggle between multiple fields from multiple blocks?
# How do you use a Matrix to toggle between multiple fields from multiple blocks?

(Asked by: username_0 on 2015-04-27)

I have 1 Matrix field:
contentBlock
With 2 Block types:
text
photo
With nested fields:
text -> text
photo -> photo
My attempt at using if statements, which didn't work, to toggle between text and photo looks like the following:
{% for block in entry.contentBlock %}
    {% if block.type == 'text' %}
        {{ block.text }}
    {% endif %}
    {% elseif block.type == 'image' %}
        {% set image = block.image.first() %}
            {% if image %}
                
            {% endif %}
{% endfor %}

Would using {% switch %} be a better method, or was I close to correct using the if statements?
I think getting the hang of this will open up the true posibilities of using Craft Matrix fields.
Thank you for any input on this question.

**Tags:** matrix, switch

---

## 1 answers

### Answer by username_1 on 2015-04-27. Score: 3 | Accepted answer

Using switch is a good way to handle this, but it looks like your conditionals are just a little off:
{% for block in entry.contentBlock %}
    {% if block.type == 'text' %}
        {{ block.text }}
    {% elseif block.type == 'photo' %}
        {% set photo = block.photo.first() %}
        {% if photo %}
            
        {% endif %}
    {% endif %}
{% endfor %}

The same thing with the switch syntax would be:
{% for block in entry.contentBlock %}

    {% switch block.type %}
        {% case "text" %}
            {{ block.text }}
        {% case "photo" %}
            {% set photo = block.photo.first() %}
            {% if photo %}
                
            {% endif %}
    {% endswitch %}

{% endfor %}

> **Comments:**
> **username_0**: I tried both of these methods and was not able to get the image/s to show using either; the text does show and seems to be the only part working. I tried amending the code for image.gerUrl to remove thumb, in case that that the issue, but that didn't work. Do I need to make further reference to where the image is located, or use a for entry in statement? Is these something missing which is assumed the user would have already included?  
> **username_1**: Is your second block named `photo` rather than `image`? If so try `case "photo"` for the second switch case. If the field in that block type is also named `photo` and not `image`, you'll need to update `block.image` to be `block.photo`.  
> **username_0**: The solution was to rename image to photo in both instances you mentioned. Thank you for helping.  
> **username_1**: You're welcome. I updated the code in my answer to use `photo` instead of `image`.  

---

