Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-28T21:31:53.403Z
Answers.Text: 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?
Answers.UserDisplayName: Craftnoob
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-28T21:39:34.383Z
Answers.Text: 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`.
Answers.UserDisplayName: Stephen Bowling
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-28T22:59:13.053Z
Answers.Text: The solution was to rename image to photo in both instances you mentioned. Thank you for helping.
Answers.UserDisplayName: Craftnoob
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-29T01:23:59.667Z
Answers.Text: You're welcome. I updated the code in my answer to use `photo` instead of `image`.
Answers.UserDisplayName: Stephen Bowling
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-27T22:17:18.373Z
Answers.LastActivityDate: 2015-04-29T01:23:18.507Z
Answers.LastEditDate: 2015-04-29T01:23:18.507Z
Answers.UserDisplayName: Stephen Bowling
Body: <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>
  
Tags: <matrix><switch>
Title: How do you use a Matrix to toggle between multiple fields from multiple blocks?
ThreadText: # 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 %}
                  <img src="{{ image.getUrl('thumb') }}" alt="{{ image.title }}">
              {% 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 %}
              <img src="{{ photo.getUrl('thumb') }}" alt="{{ photo.title }}">
          {% 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 %}
                  <img src="{{ photo.getUrl('thumb') }}" alt="{{ photo.title }}">
              {% 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`.  
  
  ---
  
  