Is it possible to cache a variable assignment and/or an element query with TWIG in Craft 3? With Yii2 this is possible, but is there a wrapper to TWIG?

2 use cases:

{% set entries = craft.entries.someThingVerySlowHere() %}
{% set myObject = customVerySlowTwigFunction(...) %}

Why would I want to do that? Let's assume I have 2 blocks in my page, both make use of the variables assigned above, then I can not cache this within the block itself (where the {% cache %} tag belongs, unless I assign/query twice.


The request makes sense, but I thought that Craft did some magic to make sure that a duplicate query/variable-assignment wasn't an issue by re-using the query internally. I have no idea why I think that though.

  • but this would only work on queries, not function calls... Aug 15 '17 at 12:20
  • of course functions could cache itself... but it's nice to see it in the template Aug 15 '17 at 12:24

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.