Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-26T12:57:01.560Z
Answers.Text: 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.
Answers.UserDisplayName: Lori
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-26T13:06:42.447Z
Answers.Text: 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.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-26T13:18:41.513Z
Answers.Text: 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.
Answers.UserDisplayName: Lori
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-26T15:36:41.730Z
Answers.Text: I figured out my additional question so I removed it from the original post. Thanks again @MatsMikkelRummelhoff for your help.
Answers.UserDisplayName: Lori
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-26T11:03:38.800Z
Answers.LastActivityDate: 2016-08-26T11:03:38.800Z
Answers.LastEditDate: 2017-05-23T12:38:53.957Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Tags: <templating><variable><logic>
Title: Reusing logic by setting different keywords to create new variables
ThreadText: # 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](https://craftcms.stackexchange.com/questions/16504/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:**
  
  ```
  <!-- Get study count for logged in sonographer -->
      {% 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' }
  
  <!-- Starting study count -->
      {% set startingCountKEYWORD = currentUser.userProfile_startingCountKEYWORD %}
      {% set startingCountAorta = currentUser.userProfile_startingCountAorta %}
      {% set startingCountCardiac = currentUser.userProfile_startingCountCardiac %}
  
  <!-- Count logged -->
      {% set countLogKEYWORD = entriesLogKEYWORD.total %}
      {% set countLogAorta = entriesLogAorta.total %}
      {% set countLogCardiac = entriesLogCardiac.total %}
  
  <!-- Count total -->
      {% set countTotalKEYWORD = countLogKEYWORD + startingCountKEYWORD %}    
      {% set countTotalAorta = countLogAorta + startingCountAorta %}
      {% set countTotalCardiac = countLogCardiac + startingCountCardiac %}
  
  ```
  
  **Output to front end:**
  
  ```
  <table class="table">
      <tbody>
          <tr>
              <td class="c-font-uppercase c-font-bold" scope="row">KEYWORD</th>
              <td class="c-center c-font-bold"> {{ countTotalKEYWORD }}</td>
          </tr>
          <tr>
              <td class="c-font-uppercase c-font-bold" scope="row">Aorta</th>
              <td class="c-center c-font-bold">{{ countTotalAorta }}</td>
          </tr>
          <tr>
              <td class="c-font-uppercase c-font-bold" scope="row">Cardiac</th>
              <td class="c-center c-font-bold">{{ countTotalCardiac }}</td>
          </tr>
      </tbody>
  </table>
  
  ```
  
  **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...
  
  <!-- Get study count for logged in sonographer -->
      {% set entriesLogKEYWORD = craft.entries.section('logsQA').search('logGeneral_studyType:KEYWORD').relatedTo(
          { targetElement: currentUser, field: 'logGeneral_sonographerName' }
      ) %}
  
  <!-- Starting study count -->
      {% set startingCountKEYWORD = currentUser.userProfile_startingCountKEYWORD %}
  
  <!-- Count logged -->
      {% set countLogKEYWORD = entriesLogKEYWORD.total %}
  
  <!-- Count total -->
      {% set countTotalKEYWORD = countLogKEYWORD + startingCountKEYWORD %}
  
  <!-- OUTPUT -->
  <table class="table">
      <tbody>
          <!-- Loop through all keyword groups? -->
          <tr>
              <td class="c-font-uppercase c-font-bold" scope="row">KEYWORD</th>
              <td class="c-center c-font-bold"> {{ countTotalKEYWORD }}</td>
          </tr>
          <!-- END Loop -->
      </tbody>
  </table>
  
  ```
  
  **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](https://stackoverflow.com/questions/10470328/how-to-combine-two-string-in-twig) 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:
  
  ```
  <table class="table">
      <tbody>
  
          {% set keywords = ['Aorta', 'Cardiac'] %}
  
          <!-- Loop through all keyword groups -->    
          {% for keyword in keywords %}
  
              <!-- Get study count for logged in sonographer -->
              {% set entriesLog = craft.entries.section('logsQA').search('logGeneral_studyType:'~keyword).relatedTo(
      { targetElement: currentUser, field: 'logGeneral_sonographerName' }
  ) %}
  
              <!-- Starting study count -->
              {% set startingCount = currentUser['userProfile_startingCount'~keyword]|default(0) %}
  
              <tr>
                  <td class="c-font-uppercase c-font-bold" scope="row">{{ keyword }}</th>
                  <td class="c-center c-font-bold">{{ entriesLog.total() + startingCount }}</td>
              </tr>
  
          {% endfor %}
          <!-- END Loop -->
  
      </tbody>
  </table>
  
  ```
  
  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, username_1, 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, @username_0 – 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, @username_1. 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 @username_1 for your help.  
  
  ---
  
  