2

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.

3

So there are some changes to how .type() works in Craft 3 which you can read about here. 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 %}
    
  • 1
    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 – Siebird Dec 19 '18 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.