Answers.Body: <p>When you call <code>entry.newsArticleContent.one().type</code>, what you're getting is an instance of <code>craft\models\MatrixBlockType</code> (<a href="https://github.com/craftcms/cms/blob/develop/src/models/MatrixBlockType.php" rel="nofollow noreferrer">https://github.com/craftcms/cms/blob/develop/src/models/MatrixBlockType.php</a>)</p>
  
  <p>It just happens to have a <a href="https://github.com/craftcms/cms/blob/develop/src/models/MatrixBlockType.php#L93-L96" rel="nofollow noreferrer"><code>__toString()</code></a> method that will print out the handle when it's requested, but the <code>if</code> comparison you're doing is using the entire <code>MatrixBlockType</code> instance.</p>
  
  <p>What you're looking for is:</p>
  
  <pre><code>{% if entry.newsArticleContent.one().type.handle  in ['fullWidthImage', 'imageGroup'] %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-11T23:37:53.353Z
Answers.LastActivityDate: 2018-09-11T23:37:53.353Z
Answers.UserDisplayName: Brad Bell
Body: <p>I need to check if the first block in a Matrix field matches one of an array of types. Here's a simplified version of my code:</p>
  
  <pre><code>{% if entry.newsArticleContent.one().type in ['fullWidthImage', 'imageGroup'] %}
      yep
  {% else %}
      nope
  {% endif %}
  </code></pre>
  
  <p><code>{{ entry.newsArticleContent.one().type }}</code> outputs <code>fullWidthImage</code>, but the above conditional outputs <code>nope</code>.</p>
  
  <p>The following outputs <code>yep</code>:</p>
  
  <pre><code>{% if 'fullWidthImage' in ['fullWidthImage', 'imageGroup'] %}
      yep
  {% else %}
      nope
  {% endif %}
  </code></pre>
  
  <p>so I know it's not a problem with the <code>if</code> statement or the fact that I'm checking the value against an array.</p>
  
  <p>Is there a reason why the value of <code>entry.newsArticleContent.one().type</code> can't be used in this context?</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT:</strong> Thought I should mention that I'm looping through all blocks in the Matrix field later on in the same template. Just in case that's relevant.</p>
  
Tags: <templating><craft3><matrix>
Title: Check if the first block in a Matrix field matches one of an array of types
ThreadText: # Check if the first block in a Matrix field matches one of an array of types
  
  (Asked by: username_0 on 2018-09-10)
  
  I need to check if the first block in a Matrix field matches one of an array of types. Here's a simplified version of my code:
  
  ```
  {% if entry.newsArticleContent.one().type in ['fullWidthImage', 'imageGroup'] %}
      yep
  {% else %}
      nope
  {% endif %}
  
  ```
  
  `{{ entry.newsArticleContent.one().type }}` outputs `fullWidthImage`, but the above conditional outputs `nope`.
  
  The following outputs `yep`:
  
  ```
  {% if 'fullWidthImage' in ['fullWidthImage', 'imageGroup'] %}
      yep
  {% else %}
      nope
  {% endif %}
  
  ```
  
  so I know it's not a problem with the `if` statement or the fact that I'm checking the value against an array.
  
  Is there a reason why the value of `entry.newsArticleContent.one().type` can't be used in this context?
  
  Thanks!
  
  **EDIT:** Thought I should mention that I'm looping through all blocks in the Matrix field later on in the same template. Just in case that's relevant.
  
  **Tags:** templating, craft3, matrix
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-09-11. Score: 1 | Accepted answer
  
  When you call `entry.newsArticleContent.one().type`, what you're getting is an instance of `craft\models\MatrixBlockType` (<https://github.com/craftcms/cms/blob/develop/src/models/MatrixBlockType.php>)
  
  It just happens to have a [`__toString()`](https://github.com/craftcms/cms/blob/develop/src/models/MatrixBlockType.php#L93-L96) method that will print out the handle when it's requested, but the `if` comparison you're doing is using the entire `MatrixBlockType` instance.
  
  What you're looking for is:
  
  ```
  {% if entry.newsArticleContent.one().type.handle  in ['fullWidthImage', 'imageGroup'] %}
  
  ```
  
  
  ---
  
  