1

I have some logic in a template where I want to count the number of items that have a lightswitch set to yes, and a radio button set to another value, currently I'm doing this via a series of {% set %} variables for loops and if statments, I just wondered if anyone know of a more efficient method to resolve this issue, or if this is the simplest implementation within Craft.

{% set normalNominations = 0 %}
{% set specialNominations = 0 %}
{% set normalWin = 0 %}
{% set specialWin = 0 %}

{% for items in entry.matrixloop %}
    {% if items.type == "awardBody" %}

    {% elseif items.type == "awardType" %}
        {% if items.oscar %}
            {% if items.awardGained == "Win" %}
                {% set specialWin = specialWin + 1 %}
            {% elseif items.awardGained == "Nomination" %}
                {% set specialNominations = specialNominations + 1 %}
            {% endif %}
        {% else %}
            {% if items.awardGained == "Win" %}
                {% set normalWin = normalWin + 1 %}
            {% elseif items.awardGained == "Nomination" %}
                {% set normalNominations = normalNominations + 1 %}
            {% endif %}
        {% endif %}
    {% endif %}
{% endfor %}

{# Debug Information #}
{#
<p> Special win: {{ specialWin }} </p>
<p> Special Nominations: {{ specialNominations }} </p>

<p> Normal Win: {{ normalWin }} </p>
<p> Normal Nomination: {{ normalNominations }} </p>

{{ entry.matrixloop | length }}

#}

Thanks in advance for your help with this, I'm pretty new to Craft so want to make sure I'm being efficient in my templates and not writing rubbish.

Ta

Dave

2

I'd say that due to the level of nested that you have going on, this is probably as compact of code as you can get out of the box. From an efficiency standpoint, you are only looping through the loop once, so I can't imagine there being anything more efficient.

If you're wanting to minimize the amount of logic in your template, then you can create a plugin and utilize template variables that would accept the matrix content as a parameter. Logic would be handled with PHP and could return an associative array of properties (specialWin, specialNominations, normalWin, normalNominations). So you'd end up with something like this:

{% set matrixData = craft.myPlugin.myPluginVariableMethod( entry.matrixloop ) %}
{# Debug Information #}
{#
<p> Special win: {{ matrixData.specialWin }} </p>
<p> Special Nominations: {{ matrixData.specialNominations }} </p>

<p> Normal Win: {{ matrixData.normalWin }} </p>
<p> Normal Nomination: {{ matrixData.normalNominations }} </p>

{{ entry.matrixloop | length }}

Personally, I'd go with the plugin route just to keep heavy logic out of the templates. It's nice that you can do that type of logic via twig if you want, but it also muddles things up a bit.

  • Awesome thanks for taking the time to look at this, I'll get on building a plugin for this :) – dave_agilepixel Aug 20 '14 at 18:04

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.