<p>Some "problems" I see with your solution:</p>

<ol>
<li>You're repeating yourself twice there - it would be much better if you had your <code>actionBilder</code> loop in there only once. When writing code, try to use the DRY principle: <strong>D</strong>on't <strong>R</strong>epeat <strong>Y</strong>ourself (obviously exceptions to that rule but this is one case where you could simplify).</li>
<li>Not sure why exactly you're using <code>exists()</code> to check you matrix block's presence. I've actually never seen that method in use before and had to look it up to see what exactly it does. Based on <a href="https://docs.craftcms.com/v3/categories-fields.html#examples" rel="nofollow noreferrer">the docs</a>, looks like you would use it to check if there are any related categories (but don’t need to fetch them). But like I said, I've never heard of <code>exists()</code> so could be I'm wrong... <em>Fwiw, here's a <a href="https://craftcms.stackexchange.com/a/842/5275">great answer</a> by Brandon Kelly on what you should use to check if a field is empty or non-existent.</em></li>
<li>This may not be a problem in your current setup but at the moment you're fetching three blocks from your matrix field and only displaying them if they are a particular type. This means if you were to ever add another block to your matrix you could end up with two, one, or even no images at all (because the three blocks you queried for are all of a different type). It would be better to grab three blocks of that particular type and then loop through them.</li>
</ol>

<p>Here's what I'd recommend doing:</p>

<pre><code>{# are we dealing with an entry page? #}
{% if entry is defined %}

    {# if so we create an array of related categories #}
    {% set categories = craft.categories.relatedTo(entry) %}

{# otherwise we'll check if this is a category page #}
{% elseif category is defined %}

    {# if so we create an array with our one category #}
    {% set categories = [ category ] %}

{% endif %}

{# make sure we actually have categories in the array #}
{% if categories | length %}

    {# start the `for` loop #}
    {% for category in categories %}

        {# does the matrix field exist and does it contain any blocks? #}
        {% if category.actionBilder | length %}

            &lt;div id="footerslide"&gt;

                {# get 3 `actionImg` blocks randomly #}
                {% for block in category.actionBilder.limit(3).type('actionImg').orderBy('RAND()').all() %}

                    {# get the block's first image's url #}
                    &lt;div data-image="{{block.image.one().url}}"&gt;&lt;/div&gt;

                {% endfor %}

            &lt;/div&gt;

        {% endif %}

    {% endfor %}

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

<p>Hope that's helpful!</p>

CC BY-SA 4.0
2018-12-12T02:26:23.727Z
2018-12-12T02:39:17.817Z
2018-12-12T02:39:17.817Z
Jalen Davenport
<p>I have a gallery in my site's <code>&lt;footer&gt;</code> with images that are defined in a matrix field on each of my categories. If I'm viewing a category page or an entry with related categories I want to display this image gallery in the footer.</p>

<p>What I currently have is this:</p>

<pre><code>{% if category is defined %}
    {% if category.actionBilder.exists() %}
        &lt;div id="footerslide"&gt;
            {% for block in category.actionBilder.limit(3).orderBy('RAND()').all() %}
                {% if block.type == "actionImg" %}
                    &lt;div data-image="{{block.image.one().url}}"&gt;&lt;/div&gt;
                {% endif %}
            {% endfor %}
        &lt;/div&gt;
    {% endif %}
{% endif %}

{% if entry is defined %}
    {% set categories = craft.categories.relatedTo(entry) %}
    {% for category in categories %}
        {% if category.actionBilder.exists() %}
            &lt;div id="footerslide"&gt;
                {% for block in category.actionBilder.limit(3).orderBy('RAND()').all() %}
                    {% if block.type == "actionImg" %}
                        &lt;div data-image="{{block.image.one().url}}"&gt;&lt;/div&gt;
                    {% endif %}
                {% endfor %}
            &lt;/div&gt;
        {% endif %}
    {% endfor %}
{% endif %}
</code></pre>

<p>While it's working fine, I don't think that this is the best solution.</p>

<p>How would you change my code to make it better?</p>

<entry><category>
Displaying gallery when viewing category page or entry with related categories - code review
# Displaying gallery when viewing category page or entry with related categories - code review

(Asked by: username_0 on 2018-11-28)

I have a gallery in my site's  with images that are defined in a matrix field on each of my categories. If I'm viewing a category page or an entry with related categories I want to display this image gallery in the footer.
What I currently have is this:
{% if category is defined %}
    {% if category.actionBilder.exists() %}
        
            {% for block in category.actionBilder.limit(3).orderBy('RAND()').all() %}
                {% if block.type == "actionImg" %}
                    
                {% endif %}
            {% endfor %}
        
    {% endif %}
{% endif %}

{% if entry is defined %}
    {% set categories = craft.categories.relatedTo(entry) %}
    {% for category in categories %}
        {% if category.actionBilder.exists() %}
            
                {% for block in category.actionBilder.limit(3).orderBy('RAND()').all() %}
                    {% if block.type == "actionImg" %}
                        
                    {% endif %}
                {% endfor %}
            
        {% endif %}
    {% endfor %}
{% endif %}

While it's working fine, I don't think that this is the best solution.
How would you change my code to make it better?

**Tags:** entry, category

---

## 1 answers

### Answer by username_1 on 2018-12-12. Score: 1

Some "problems" I see with your solution:

You're repeating yourself twice there - it would be much better if you had your actionBilder loop in there only once. When writing code, try to use the DRY principle: Don't Repeat Yourself (obviously exceptions to that rule but this is one case where you could simplify).
Not sure why exactly you're using exists() to check you matrix block's presence. I've actually never seen that method in use before and had to look it up to see what exactly it does. Based on the docs, looks like you would use it to check if there are any related categories (but don’t need to fetch them). But like I said, I've never heard of exists() so could be I'm wrong... Fwiw, here's a great answer by Brandon Kelly on what you should use to check if a field is empty or non-existent.
This may not be a problem in your current setup but at the moment you're fetching three blocks from your matrix field and only displaying them if they are a particular type. This means if you were to ever add another block to your matrix you could end up with two, one, or even no images at all (because the three blocks you queried for are all of a different type). It would be better to grab three blocks of that particular type and then loop through them.

Here's what I'd recommend doing:
{# are we dealing with an entry page? #}
{% if entry is defined %}

    {# if so we create an array of related categories #}
    {% set categories = craft.categories.relatedTo(entry) %}

{# otherwise we'll check if this is a category page #}
{% elseif category is defined %}

    {# if so we create an array with our one category #}
    {% set categories = [ category ] %}

{% endif %}

{# make sure we actually have categories in the array #}
{% if categories | length %}

    {# start the `for` loop #}
    {% for category in categories %}

        {# does the matrix field exist and does it contain any blocks? #}
        {% if category.actionBilder | length %}

            

                {# get 3 `actionImg` blocks randomly #}
                {% for block in category.actionBilder.limit(3).type('actionImg').orderBy('RAND()').all() %}

                    {# get the block's first image's url #}
                    

                {% endfor %}

            

        {% endif %}

    {% endfor %}

{% endif %}

Hope that's helpful!


---

