<p>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 <em>no idea</em> why I think that though.</p>

CC BY-SA 3.0
2017-08-15T12:20:20.933Z
but this would only work on queries, not function calls...
Matthias Redl-Mann
CC BY-SA 3.0
2017-08-15T12:24:19.980Z
of course functions could cache itself... but it's nice to see it in the template
Matthias Redl-Mann
CC BY-SA 3.0
2017-08-15T10:20:02.247Z
2017-08-15T10:20:02.247Z
Matt Wilcox
<p>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?</p>

<p>2 use cases:</p>

<pre><code>{% set entries = craft.entries.someThingVerySlowHere() %}
{% set myObject = customVerySlowTwigFunction(...) %}
</code></pre>

<p>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 <code>{% cache %}</code> tag belongs, unless I assign/query twice.</p>

<templating><cache><craft3>
Craft 3: Cache element query or variable assignment
# Craft 3: Cache element query or variable assignment

(Asked by: username_0 on 2017-08-15)

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.

**Tags:** templating, cache, craft3


---

## 1 answers

### Answer by username_1 on 2017-08-15. Score: -1

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.

> **Comments:**
> **username_0**: but this would only work on queries, not function calls...  
> **username_0**: of course functions could cache itself... but it's nice to see it in the template  

---

