3

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.

  • Is this all within set tags like this: {% set images %}{% endset %}? Didn't know you can define arrays doing so.. – carlcs Oct 27 '14 at 16:36
  • 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 Oct 27 '14 at 18:07
  • 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 Oct 27 '14 at 18:20
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 %}

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.