<p>If you use brackets notation instead of the usual dot notation to access your different properties, you can <a href="https://stackoverflow.com/questions/10470328/how-to-combine-two-string-in-twig">append</a> a <code>keyword</code> variable to the property handle:</p>

<pre><code>{% set startingCount = currentUser['userProfile_startingCount'~keyword] %}
</code></pre>

<p>Of course, you can use the <code>~</code> operator to append a keyword to your <code>craft.entries</code> <code>.search()</code> parameter as well:</p>

<pre><code>{% set entriesLog = craft.entries.section('logsQA').search('logGeneral_studyType:'~keyword) ... %}
</code></pre>

<p>So with that in mind, it'd probably make sense to create a simple array with your keywords, and just loop over them like this:</p>

<pre><code>&lt;table class="table"&gt;
    &lt;tbody&gt;

        {% set keywords = ['Aorta', 'Cardiac'] %}

        &lt;!-- Loop through all keyword groups --&gt;    
        {% for keyword in keywords %}

            &lt;!-- Get study count for logged in sonographer --&gt;
            {% set entriesLog = craft.entries.section('logsQA').search('logGeneral_studyType:'~keyword).relatedTo(
    { targetElement: currentUser, field: 'logGeneral_sonographerName' }
) %}

            &lt;!-- Starting study count --&gt;
            {% set startingCount = currentUser['userProfile_startingCount'~keyword]|default(0) %}

            &lt;tr&gt;
                &lt;td class="c-font-uppercase c-font-bold" scope="row"&gt;{{ keyword }}&lt;/th&gt;
                &lt;td class="c-center c-font-bold"&gt;{{ entriesLog.total() + startingCount }}&lt;/td&gt;
            &lt;/tr&gt;

        {% endfor %}
        &lt;!-- END Loop --&gt;

    &lt;/tbody&gt;
&lt;/table&gt;
</code></pre>

<p>Note: I've omitted the <code>countLog</code> and <code>countTotal</code> variables completely in the above example; unless those two variables are going to be re-used, in this case its perfectly fine to just add up <code>entriesLog.total() + startingCount</code> when you echo the value, saving a few lines of code.</p>

<p>Also note that I've added a <code>|default(0)</code> to the <code>startingCount</code> variable, just in case the <code>'userProfile_startingCount'~keyword</code> property doesn't exist for <code>currentUser</code> (in which case you'd probably get a template error).</p>

CC BY-SA 3.0
2016-08-26T12:57:01.560Z
Thank you so much, Mats, that worked perfectly to get my total studies.  I have another template that I'm doing something similar but I can't get it to work. I will edit my original so I can include the code.
Lori
CC BY-SA 3.0
2016-08-26T13:06:42.447Z
That's great, @Lori – happy it helped you out. It might be a good idea to create a new question for your other template, unless that issue is specifically related to this one.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-26T13:18:41.513Z
Thanks, @MatsMikkelRummelhoff. I edited this post because the two templates are almost identical and using the same types of variables.  I hope that was the correct thing to do.
Lori
CC BY-SA 3.0
2016-08-26T15:36:41.730Z
I figured out my additional question so I removed it from the original post. Thanks again @MatsMikkelRummelhoff for your help.
Lori
CC BY-SA 3.0
2016-08-26T11:03:38.800Z
2016-08-26T11:03:38.800Z
2017-05-23T12:38:53.957Z
Mats Mikkel Rummelhoff
<p>I need to reuse logic for 15 entry groupings in a template.  In my non DRY version, I simply changed one word in the variable name to create a new variable and although it works just fine, it's very cumbersome to make changes because if I need to make a logic change, I have to do it for all 15 groupings. I'm wondering if there's a way to create a main variable group where I could specify each variable keyword once and then have that keyword replaced in the code. I found a similar post: <a href="https://craftcms.stackexchange.com/questions/16504/reusing-logic-to-set-string-variables">Reusing logic to set string variables</a></p>

<p>Here's a sample of the code in non DRY fashion. You'll notice the code is almost identical except for the words "Aorta" and "Cardiac".  I'm treating each of those words as their respective grouping keyword (the top example in each section shows you were the KEYWORD is).</p>

<p><strong>Set the variables:</strong></p>

<pre><code>&lt;!-- Get study count for logged in sonographer --&gt;
    {% set entriesLogKEYWORD = craft.entries.section('logsQA').search('logGeneral_studyType:KEYWORD').relatedTo(
        { targetElement: currentUser, field: 'logGeneral_sonographerName' }
    ) %}
    {% set entriesLogAorta = craft.entries.section('logsQA').search('logGeneral_studyType:Aorta').relatedTo(
        { targetElement: currentUser, field: 'logGeneral_sonographerName' }
    ) %}
    {% set entriesLogCardiac = craft.entries.section('logsQA').search('logGeneral_studyType:Cardiac').relatedTo(
        { targetElement: currentUser, field: 'logGeneral_sonographerName' }

&lt;!-- Starting study count --&gt;
    {% set startingCountKEYWORD = currentUser.userProfile_startingCountKEYWORD %}
    {% set startingCountAorta = currentUser.userProfile_startingCountAorta %}
    {% set startingCountCardiac = currentUser.userProfile_startingCountCardiac %}

&lt;!-- Count logged --&gt;
    {% set countLogKEYWORD = entriesLogKEYWORD.total %}
    {% set countLogAorta = entriesLogAorta.total %}
    {% set countLogCardiac = entriesLogCardiac.total %}

&lt;!-- Count total --&gt;
    {% set countTotalKEYWORD = countLogKEYWORD + startingCountKEYWORD %}    
    {% set countTotalAorta = countLogAorta + startingCountAorta %}
    {% set countTotalCardiac = countLogCardiac + startingCountCardiac %}
</code></pre>

<p><strong>Output to front end:</strong></p>

<pre><code>&lt;table class="table"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td class="c-font-uppercase c-font-bold" scope="row"&gt;KEYWORD&lt;/th&gt;
            &lt;td class="c-center c-font-bold"&gt; {{ countTotalKEYWORD }}&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="c-font-uppercase c-font-bold" scope="row"&gt;Aorta&lt;/th&gt;
            &lt;td class="c-center c-font-bold"&gt;{{ countTotalAorta }}&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="c-font-uppercase c-font-bold" scope="row"&gt;Cardiac&lt;/th&gt;
            &lt;td class="c-center c-font-bold"&gt;{{ countTotalCardiac }}&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
</code></pre>

<p><strong>Possible to set the keyword variable and then call it elsewhere like this so template would look something like this where I'd only be writing the logic code once?</strong></p>

<pre><code>{% set KEYWORD = "Aorta %}
{% set KEYWORD = "Cardiac" %}
etc...

&lt;!-- Get study count for logged in sonographer --&gt;
    {% set entriesLogKEYWORD = craft.entries.section('logsQA').search('logGeneral_studyType:KEYWORD').relatedTo(
        { targetElement: currentUser, field: 'logGeneral_sonographerName' }
    ) %}

&lt;!-- Starting study count --&gt;
    {% set startingCountKEYWORD = currentUser.userProfile_startingCountKEYWORD %}

&lt;!-- Count logged --&gt;
    {% set countLogKEYWORD = entriesLogKEYWORD.total %}

&lt;!-- Count total --&gt;
    {% set countTotalKEYWORD = countLogKEYWORD + startingCountKEYWORD %}

&lt;!-- OUTPUT --&gt;
&lt;table class="table"&gt;
    &lt;tbody&gt;
        &lt;!-- Loop through all keyword groups? --&gt;
        &lt;tr&gt;
            &lt;td class="c-font-uppercase c-font-bold" scope="row"&gt;KEYWORD&lt;/th&gt;
            &lt;td class="c-center c-font-bold"&gt; {{ countTotalKEYWORD }}&lt;/td&gt;
        &lt;/tr&gt;
        &lt;!-- END Loop --&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
</code></pre>

<templating><variable><logic>
Reusing logic by setting different keywords to create new variables
# Reusing logic by setting different keywords to create new variables

(Asked by: username_0 on 2016-08-26)

I need to reuse logic for 15 entry groupings in a template.  In my non DRY version, I simply changed one word in the variable name to create a new variable and although it works just fine, it's very cumbersome to make changes because if I need to make a logic change, I have to do it for all 15 groupings. I'm wondering if there's a way to create a main variable group where I could specify each variable keyword once and then have that keyword replaced in the code. I found a similar post: Reusing logic to set string variables
Here's a sample of the code in non DRY fashion. You'll notice the code is almost identical except for the words "Aorta" and "Cardiac".  I'm treating each of those words as their respective grouping keyword (the top example in each section shows you were the KEYWORD is).
Set the variables:

    {% set entriesLogKEYWORD = craft.entries.section('logsQA').search('logGeneral_studyType:KEYWORD').relatedTo(
        { targetElement: currentUser, field: 'logGeneral_sonographerName' }
    ) %}
    {% set entriesLogAorta = craft.entries.section('logsQA').search('logGeneral_studyType:Aorta').relatedTo(
        { targetElement: currentUser, field: 'logGeneral_sonographerName' }
    ) %}
    {% set entriesLogCardiac = craft.entries.section('logsQA').search('logGeneral_studyType:Cardiac').relatedTo(
        { targetElement: currentUser, field: 'logGeneral_sonographerName' }


    {% set startingCountKEYWORD = currentUser.userProfile_startingCountKEYWORD %}
    {% set startingCountAorta = currentUser.userProfile_startingCountAorta %}
    {% set startingCountCardiac = currentUser.userProfile_startingCountCardiac %}


    {% set countLogKEYWORD = entriesLogKEYWORD.total %}
    {% set countLogAorta = entriesLogAorta.total %}
    {% set countLogCardiac = entriesLogCardiac.total %}


    {% set countTotalKEYWORD = countLogKEYWORD + startingCountKEYWORD %}    
    {% set countTotalAorta = countLogAorta + startingCountAorta %}
    {% set countTotalCardiac = countLogCardiac + startingCountCardiac %}

Output to front end:

    
        
            KEYWORD
             {{ countTotalKEYWORD }}
        
        
            Aorta
            {{ countTotalAorta }}
        
        
            Cardiac
            {{ countTotalCardiac }}
        
    


Possible to set the keyword variable and then call it elsewhere like this so template would look something like this where I'd only be writing the logic code once?
{% set KEYWORD = "Aorta %}
{% set KEYWORD = "Cardiac" %}
etc...


    {% set entriesLogKEYWORD = craft.entries.section('logsQA').search('logGeneral_studyType:KEYWORD').relatedTo(
        { targetElement: currentUser, field: 'logGeneral_sonographerName' }
    ) %}


    {% set startingCountKEYWORD = currentUser.userProfile_startingCountKEYWORD %}


    {% set countLogKEYWORD = entriesLogKEYWORD.total %}


    {% set countTotalKEYWORD = countLogKEYWORD + startingCountKEYWORD %}



    
        
        
            KEYWORD
             {{ countTotalKEYWORD }}

**Tags:** templating, variable, logic

---

## 1 answers

### Answer by username_1 on 2016-08-26. Score: 1 | Accepted answer

If you use brackets notation instead of the usual dot notation to access your different properties, you can append a keyword variable to the property handle:
{% set startingCount = currentUser['userProfile_startingCount'~keyword] %}

Of course, you can use the ~ operator to append a keyword to your craft.entries .search() parameter as well:
{% set entriesLog = craft.entries.section('logsQA').search('logGeneral_studyType:'~keyword) ... %}

So with that in mind, it'd probably make sense to create a simple array with your keywords, and just loop over them like this:

    

        {% set keywords = ['Aorta', 'Cardiac'] %}

            
        {% for keyword in keywords %}

            
            {% set entriesLog = craft.entries.section('logsQA').search('logGeneral_studyType:'~keyword).relatedTo(
    { targetElement: currentUser, field: 'logGeneral_sonographerName' }
) %}

            
            {% set startingCount = currentUser['userProfile_startingCount'~keyword]|default(0) %}

            
                {{ keyword }}
                {{ entriesLog.total() + startingCount }}
            

        {% endfor %}
        

    


Note: I've omitted the countLog and countTotal variables completely in the above example; unless those two variables are going to be re-used, in this case its perfectly fine to just add up entriesLog.total() + startingCount when you echo the value, saving a few lines of code.
Also note that I've added a |default(0) to the startingCount variable, just in case the 'userProfile_startingCount'~keyword property doesn't exist for currentUser (in which case you'd probably get a template error).

> **Comments:**
> **username_0**: Thank you so much, Mats, that worked perfectly to get my total studies.  I have another template that I'm doing something similar but I can't get it to work. I will edit my original so I can include the code.  
> **username_1**: That's great, @Lori – happy it helped you out. It might be a good idea to create a new question for your other template, unless that issue is specifically related to this one.  
> **username_0**: Thanks, @MatsMikkelRummelhoff. I edited this post because the two templates are almost identical and using the same types of variables.  I hope that was the correct thing to do.  
> **username_0**: I figured out my additional question so I removed it from the original post. Thanks again @MatsMikkelRummelhoff for your help.  

---

