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:
<!-- 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>