Answers.Body: <p>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. </p>
  
  <p>If you're wanting to minimize the amount of logic in your template, then you can create a plugin and utilize <a href="http://buildwithcraft.com/docs/plugins/variables" rel="nofollow">template variables</a> 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:</p>
  
  <pre><code>{% set matrixData = craft.myPlugin.myPluginVariableMethod( entry.matrixloop ) %}
  {# Debug Information #}
  {#
  &lt;p&gt; Special win: {{ matrixData.specialWin }} &lt;/p&gt;
  &lt;p&gt; Special Nominations: {{ matrixData.specialNominations }} &lt;/p&gt;
  
  &lt;p&gt; Normal Win: {{ matrixData.normalWin }} &lt;/p&gt;
  &lt;p&gt; Normal Nomination: {{ matrixData.normalNominations }} &lt;/p&gt;
  
  {{ entry.matrixloop | length }}
  </code></pre>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-20T18:04:39.860Z
Answers.Text: Awesome thanks for taking the time to look at this, I'll get on building a plugin for this :)
Answers.UserDisplayName: dave_agilepixel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-19T16:17:37.807Z
Answers.LastActivityDate: 2014-08-19T16:17:37.807Z
Answers.UserDisplayName: Wes Rice
Body: <p>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.</p>
  
  <pre><code>{% 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 #}
  {#
  &lt;p&gt; Special win: {{ specialWin }} &lt;/p&gt;
  &lt;p&gt; Special Nominations: {{ specialNominations }} &lt;/p&gt;
  
  &lt;p&gt; Normal Win: {{ normalWin }} &lt;/p&gt;
  &lt;p&gt; Normal Nomination: {{ normalNominations }} &lt;/p&gt;
  
  {{ entry.matrixloop | length }}
  
  #}
  </code></pre>
  
  <p>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.</p>
  
  <p>Ta</p>
  
  <p>Dave</p>
  
Tags: <matrix><forloop>
Title: More efficient matrix count method
ThreadText: # More efficient matrix count method
  
  (Asked by: username_0 on 2014-08-18)
  
  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
  
  **Tags:** matrix, forloop
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-08-19. Score: 2 | Accepted answer
  
  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](http://buildwithcraft.com/docs/plugins/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.
  
  > **Comments:**
  > **username_0**: Awesome thanks for taking the time to look at this, I'll get on building a plugin for this :)  
  
  ---
  
  