<p>What about composing a custom Twig array containing all the data you need. This does all your DB queries and because you write it in default sort order (randomize afterwards), you can cache all the heavy lifting of your template: </p>

<pre><code>{# Cache an array of fake "ElementModels" #}
{% cache %}

{% set imagesArray = [] %}
{% set tagsColor = [] %}

{% for image in craft.assets %}

    {% for tag in image.tagsColor %}

        {% set tagColor = {
            slug: tag|slugify,
            tag: tag,
        } %}

        {% set tagsColor = tagsColor|merge(tagColor) %}

    {% endfor %}

    {% set imageObject = {
        url: image.url,
        title: image.title,
        tagsColor: tagsColor,
    } %}

    {% set imagesArray = imagesArray|merge(imageObject) %}

{% endfor %}

{% endcache %}
</code></pre>

<p>Then randomize your images and print the JSON:</p>

<pre><code>{# Randomize our array #}
{% set imagesArray = shuffle(imagesArray) %}

{# Print the thing into our JSON #}
{% for image in imagesArray %}
    ...
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-10-27T16:59:53.037Z
2014-10-28T08:25:42.150Z
2014-10-28T08:25:42.150Z
carlcs
<p>I'm trying to determine if I can update this:</p>

<pre><code>{% cache %}
[
{% set tags = craft.tags.group('color','season','style','venue').limit(null) %}
{% for image in craft.assets.relatedTo(tags).order('elements.dateCreated desc') %}
{% set thumbParams = {
    width: 300,
    height:300,
    mode: 'crop'
} %}
{% set smallParams = {
    width: 768,
    mode: 'fit'
} %}
    {
        "url": "{{ image.url }}",
        "filename": "{{ image.filename }}",
        "imageTitle": "{{ image.imageTitle }}",
        "imageThumb": "{{ image.getUrl(thumbParams) }}",
        "tagsColor": [
{% for tag in image.tagsColor %}
        {
            "slug": "{{ tag | slugify }}",
            "tag":  "{{ tag }}"
        }{% if not (loop.last) %},{% endif %}

{% endfor %}
        ],

        "tagsSeason": [
{% for tag in image.tagsSeason %}
            {
            "slug": "{{ tag | slugify }}",
            "tag":  "{{ tag }}"
            }{% if not (loop.last) %},{% endif %}

{% endfor %}
        ],

        "tagsStyle": [
{% for tag in image.tagsStyle %}
            {
            "slug": "{{ tag | slugify }}",
            "tag":  "{{ tag }}"
            }{% if not (loop.last) %},{% endif %}

{% endfor %}
        ],

        "tagsVenue": [
{% for tag in image.tagsVenue %}
            {
            "slug": "{{ tag | slugify }}",
            "tag":  "{{ tag }}"
            }{% if not (loop.last) %},{% endif %}

{% endfor %}
        ]

    }{% if not loop.last %},
{% endif %}

{% endfor %}
]
{% endcache %}
</code></pre>

<p>To randomize the order of images. Is that possible with <code>.order</code> and is that possible within cached template code? (As random would be "new" every instance.)</p>

<p>Note that I have the image crops and tag lookups inside the cached code, as those are intensive. The first answer, to shuffle the images after fetching them within the cached code, won't work, because the <code>for</code> loop on the image set is what needs to be cached.</p>

CC BY-SA 3.0
2014-10-27T16:36:31.777Z
Is this all within `set` tags like this: `{% set images %}{% endset %}`? Didn't know you can define arrays doing so..
carlcs
CC BY-SA 3.0
2014-10-27T18:07:37.260Z
The array code is not Twig syntax. This is a JSON template. It's building up a JSON array which we fetch with asynch JavaScript from another page.
Mark J. Reeves
CC BY-SA 3.0
2014-10-27T18:20:38.007Z
Ouuups.. my mistake! :) But maybe that's what you could do to cache the data from your loops etc., to put all the data in a new Twig array that you then randomize before "printing" your son array?!
carlcs
<templating><cache><ordering><random>
Can random be used for ordering with related/assets, in cached code?
# Can random be used for ordering with related/assets, in cached code?

(Asked by: username_0 on 2014-10-24)

I'm trying to determine if I can update this:
{% cache %}
[
{% set tags = craft.tags.group('color','season','style','venue').limit(null) %}
{% for image in craft.assets.relatedTo(tags).order('elements.dateCreated desc') %}
{% set thumbParams = {
    width: 300,
    height:300,
    mode: 'crop'
} %}
{% set smallParams = {
    width: 768,
    mode: 'fit'
} %}
    {
        "url": "{{ image.url }}",
        "filename": "{{ image.filename }}",
        "imageTitle": "{{ image.imageTitle }}",
        "imageThumb": "{{ image.getUrl(thumbParams) }}",
        "tagsColor": [
{% for tag in image.tagsColor %}
        {
            "slug": "{{ tag | slugify }}",
            "tag":  "{{ tag }}"
        }{% if not (loop.last) %},{% endif %}

{% endfor %}
        ],

        "tagsSeason": [
{% for tag in image.tagsSeason %}
            {
            "slug": "{{ tag | slugify }}",
            "tag":  "{{ tag }}"
            }{% if not (loop.last) %},{% endif %}

{% endfor %}
        ],

        "tagsStyle": [
{% for tag in image.tagsStyle %}
            {
            "slug": "{{ tag | slugify }}",
            "tag":  "{{ tag }}"
            }{% if not (loop.last) %},{% endif %}

{% endfor %}
        ],

        "tagsVenue": [
{% for tag in image.tagsVenue %}
            {
            "slug": "{{ tag | slugify }}",
            "tag":  "{{ tag }}"
            }{% if not (loop.last) %},{% endif %}

{% endfor %}
        ]

    }{% if not loop.last %},
{% endif %}

{% endfor %}
]
{% endcache %}

To randomize the order of images. Is that possible with .order and is that possible within cached template code? (As random would be "new" every instance.)
Note that I have the image crops and tag lookups inside the cached code, as those are intensive. The first answer, to shuffle the images after fetching them within the cached code, won't work, because the for loop on the image set is what needs to be cached.

**Tags:** templating, cache, ordering, random

> **Comments:**
> **username_1**: Is this all within `set` tags like this: `{% set images %}{% endset %}`? Didn't know you can define arrays doing so..  
> **username_0**: The array code is not Twig syntax. This is a JSON template. It's building up a JSON array which we fetch with asynch JavaScript from another page.  
> **username_1**: Ouuups.. my mistake! :) But maybe that's what you could do to cache the data from your loops etc., to put all the data in a new Twig array that you then randomize before "printing" your son array?!  

---

## 1 answers

### Answer by username_1 on 2014-10-27. Score: 1

What about composing a custom Twig array containing all the data you need. This does all your DB queries and because you write it in default sort order (randomize afterwards), you can cache all the heavy lifting of your template: 
{# Cache an array of fake "ElementModels" #}
{% cache %}

{% set imagesArray = [] %}
{% set tagsColor = [] %}

{% for image in craft.assets %}

    {% for tag in image.tagsColor %}

        {% set tagColor = {
            slug: tag|slugify,
            tag: tag,
        } %}

        {% set tagsColor = tagsColor|merge(tagColor) %}

    {% endfor %}

    {% set imageObject = {
        url: image.url,
        title: image.title,
        tagsColor: tagsColor,
    } %}

    {% set imagesArray = imagesArray|merge(imageObject) %}

{% endfor %}

{% endcache %}

Then randomize your images and print the JSON:
{# Randomize our array #}
{% set imagesArray = shuffle(imagesArray) %}

{# Print the thing into our JSON #}
{% for image in imagesArray %}
    ...
{% endfor %}


---

