Answers.Body: <p>So there are some changes to how <code>.type()</code> works in Craft 3 which you can read about <a href="https://docs.craftcms.com/v3/changes-in-craft-3.html#cloning-element-queries" rel="nofollow noreferrer">here</a>. What's happening is the <code>type()</code> you're setting here in your <code>doNotCache</code> is also getting applied later on in your template when you start actually looping over the blocks in <code>entry.mainBody</code>. There are two ways to solve this:</p>
  
  <ol>
  <li><p>Reset the <code>type()</code> in your matrix block loop</p>
  
  <pre><code>{% for block in entry.mainBody.type(null).all() %}
  </code></pre></li>
  <li><p>Clone the <code>mainBody</code> field before you manipulate it</p>
  
  <pre><code>{% set doNotCache = clone(entry.mainBody).type('form').all()|length or craft.retour.getHttpStatus != 200 ? true : false %}
  </code></pre></li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-19T21:31:02.863Z
Answers.Text: I appreciate the help Jalen. Always good to learn new "gotchas" on how Craft 3 handles the `.type()` parameter method. I'm using the second option cloning the matrix field
Answers.UserDisplayName: Siebird
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-19T21:26:25.527Z
Answers.LastActivityDate: 2018-12-19T21:26:25.527Z
Answers.UserDisplayName: Jalen Davenport
Body: <p>Not sure if this is a bug or my logic is off. I'm trying to add caching logic to templates to not cache certain pages if they have a block type <code>form</code> present because of the dynamic <code>CSRF</code> tokens. </p>
  
  <p>The logic:</p>
  
  <pre><code>{% set doNotCache = entry.mainBody.type('form').all()|length or craft.retour.getHttpStatus != 200 ? true : false %}
  </code></pre>
  
  <p>If the page returns <code>true</code>, the page renders properly. If the logic returns <code>false</code> the <code>entry.mainBody</code> object is empty (ie. doesn't render the matrix content). If I remove the logic all together the pages render properly. </p>
  
Tags: <craft3><matrix>
Title: Empty matrix object after checking for a block type
ThreadText: # Empty matrix object after checking for a block type
  
  (Asked by: username_0 on 2018-12-19)
  
  Not sure if this is a bug or my logic is off. I'm trying to add caching logic to templates to not cache certain pages if they have a block type `form` present because of the dynamic `CSRF` tokens.
  
  The logic:
  
  ```
  {% set doNotCache = entry.mainBody.type('form').all()|length or craft.retour.getHttpStatus != 200 ? true : false %}
  
  ```
  
  If the page returns `true`, the page renders properly. If the logic returns `false` the `entry.mainBody` object is empty (ie. doesn't render the matrix content). If I remove the logic all together the pages render properly.
  
  **Tags:** craft3, matrix
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-12-19. Score: 3 | Accepted answer
  
  So there are some changes to how `.type()` works in Craft 3 which you can read about [here](https://docs.craftcms.com/v3/changes-in-craft-3.html#cloning-element-queries). What's happening is the `type()` you're setting here in your `doNotCache` is also getting applied later on in your template when you start actually looping over the blocks in `entry.mainBody`. There are two ways to solve this:
  
  1. Reset the `type()` in your matrix block loop
  
     ```
     {% for block in entry.mainBody.type(null).all() %}
  
     ```
  2. Clone the `mainBody` field before you manipulate it
  
     ```
     {% set doNotCache = clone(entry.mainBody).type('form').all()|length or craft.retour.getHttpStatus != 200 ? true : false %}
  
     ```
  
  > **Comments:**
  > **username_0**: I appreciate the help username_1. Always good to learn new "gotchas" on how Craft 3 handles the `.type()` parameter method. I'm using the second option cloning the matrix field  
  
  ---
  
  