<p>I would use <a href="http://twig.sensiolabs.org/doc/tags/macro.html" rel="nofollow">macros</a>. A Twig macro is basically a function, that can take arguments and render HTML.</p>

<p>Macros can be defined inline (i.e. in the same template where it's used) or in a separate file (better if you're writing a generic macro that'll possibly be used in several different templates).</p>

<p>Here's how your template could look, using macros:</p>

<pre><code>{% macro courseName(entry) %}
    {{ entry.deptCode ~ ' ' ~ entry.courseNumber ~ ': ' ~ entry.title }}
{% endmacro %}

{% macro msuLink(entry) %}
    {{ 'https://reg.msu.edu/Courses/Request.aspx?SubjectCode=' ~
entry.deptCode ~ '&amp;CourseNumber=' ~ entry.courseNumber ~ '#Results' }}
{% endmacro %}

{% import _self as helpers %}

&lt;h2&gt;Required Courses&lt;/h2&gt;
&lt;ul&gt;
    {% for entry in craft.entries.section('courses').type('safsCourse') %}
        &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ helpers.courseName(entry) }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;

&lt;h2&gt;Elective Courses&lt;/h2&gt;

&lt;h3&gt;At least one agricultural science course&lt;/h3&gt;
&lt;ul&gt;
    {% for entry in craft.entries.relatedTo(65) %}
        &lt;li&gt;&lt;a href="{{ helpers.msuLink(entry) }}"&gt;{{ helpers.courseName(entry) }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;

...
</code></pre>

<p>If you're wondering about the <code>{% import _self as helpers %}</code> directive below the actual macros, the <code>{% import %}</code> tag is neccessary to actually expose your macros to your template (even if the macros are declared in the same file), and the "helpers" part is just a contextual variable that could be named anything you want (as semantic as possible is always a good thing).</p>

CC BY-SA 3.0
2016-08-24T23:43:28.720Z
Oh! Kind of like helpers in Ruby. This solution worked great. I put the macros in their own file so I can add to it later and maybe use them in other templates. Thank you!
Katie Fritz
CC BY-SA 3.0
2016-08-24T22:29:10.747Z
2016-08-24T22:29:10.747Z
Mats Mikkel Rummelhoff
<p>I would like to set some variables for every entry within a section ('courses'), and then use those variables in loops that are filtering by entry types and categories.</p>

<p>Some simplified markup:</p>

<pre><code>&lt;h2&gt;Required Courses&lt;/h2&gt;
&lt;ul&gt;
  {% for entry in craft.entries.section('courses').type('safsCourse') %}
    &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ courseName }}&lt;/a&gt;&lt;/li&gt;
  {% endfor %}
&lt;/ul&gt;

&lt;h2&gt;Elective Courses&lt;/h2&gt;

    &lt;h3&gt;At least one agricultural science course&lt;/h3&gt;
    &lt;ul&gt;
      {% for entry in craft.entries.relatedTo(65) %}
        &lt;li&gt;&lt;a href="{{ msuLink }}"&gt;{{ courseName }}&lt;/a&gt;&lt;/li&gt;
      {% endfor %}
    &lt;/ul&gt;

    &lt;h3&gt;At least one social science course (3cr)&lt;/h3&gt;
    &lt;ul&gt;
      {% for entry in craft.entries.relatedTo(66) %}
        &lt;li&gt;a href="{{ msuLink }}"&gt;{{ courseName }}&lt;/a&gt;&lt;/li&gt;
      {% endfor %}
    &lt;/ul&gt;
</code></pre>

<p>As you can see, I'm using the variables <code>msulink</code> and <code>courseName</code>. The code works as intended if I include the following within each <code>{% for entry... %}</code> block:</p>

<pre><code>{% set courseName = entry.deptCode ~ ' ' ~ entry.courseNumber ~ ': ' ~ entry.title %}
{% set msuLink = 'https://reg.msu.edu/Courses/Request.aspx?SubjectCode=' ~
    entry.deptCode ~ '&amp;CourseNumber=' ~ entry.courseNumber ~ '#Results' %}
</code></pre>

<p>But I don't want to set those variables three identical times. </p>

<p><strong>How can I set those variables once and use them when I'm pulling various subsets of my Course section entries?</strong></p>

CC BY-SA 3.0
2016-08-24T22:17:09.140Z
By the way, I know I'm probably not using the right vocabulary to describe what I'm trying to do. If you can't answer the question but can help me be more precise with my language, I'd still appreciate it!
Katie Fritz
<templating><structure><filter><variable><index>
Reusing logic to set string variables
# Reusing logic to set string variables

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

I would like to set some variables for every entry within a section ('courses'), and then use those variables in loops that are filtering by entry types and categories.
Some simplified markup:
Required Courses

  {% for entry in craft.entries.section('courses').type('safsCourse') %}
    {{ courseName }}
  {% endfor %}


Elective Courses

    At least one agricultural science course
    
      {% for entry in craft.entries.relatedTo(65) %}
        {{ courseName }}
      {% endfor %}
    

    At least one social science course (3cr)
    
      {% for entry in craft.entries.relatedTo(66) %}
        a href="{{ msuLink }}">{{ courseName }}
      {% endfor %}
    

As you can see, I'm using the variables msulink and courseName. The code works as intended if I include the following within each {% for entry... %} block:
{% set courseName = entry.deptCode ~ ' ' ~ entry.courseNumber ~ ': ' ~ entry.title %}
{% set msuLink = 'https://reg.msu.edu/Courses/Request.aspx?SubjectCode=' ~
    entry.deptCode ~ '&CourseNumber=' ~ entry.courseNumber ~ '#Results' %}

But I don't want to set those variables three identical times. 
How can I set those variables once and use them when I'm pulling various subsets of my Course section entries?

**Tags:** templating, structure, filter, variable, index

> **Comments:**
> **username_0**: By the way, I know I'm probably not using the right vocabulary to describe what I'm trying to do. If you can't answer the question but can help me be more precise with my language, I'd still appreciate it!  

---

## 1 answers

### Answer by username_1 on 2016-08-24. Score: 4 | Accepted answer

I would use macros. A Twig macro is basically a function, that can take arguments and render HTML.
Macros can be defined inline (i.e. in the same template where it's used) or in a separate file (better if you're writing a generic macro that'll possibly be used in several different templates).
Here's how your template could look, using macros:
{% macro courseName(entry) %}
    {{ entry.deptCode ~ ' ' ~ entry.courseNumber ~ ': ' ~ entry.title }}
{% endmacro %}

{% macro msuLink(entry) %}
    {{ 'https://reg.msu.edu/Courses/Request.aspx?SubjectCode=' ~
entry.deptCode ~ '&CourseNumber=' ~ entry.courseNumber ~ '#Results' }}
{% endmacro %}

{% import _self as helpers %}

Required Courses

    {% for entry in craft.entries.section('courses').type('safsCourse') %}
        {{ helpers.courseName(entry) }}
    {% endfor %}


Elective Courses

At least one agricultural science course

    {% for entry in craft.entries.relatedTo(65) %}
        {{ helpers.courseName(entry) }}
    {% endfor %}


...

If you're wondering about the {% import _self as helpers %} directive below the actual macros, the {% import %} tag is neccessary to actually expose your macros to your template (even if the macros are declared in the same file), and the "helpers" part is just a contextual variable that could be named anything you want (as semantic as possible is always a good thing).

> **Comments:**
> **username_0**: Oh! Kind of like helpers in Ruby. This solution worked great. I put the macros in their own file so I can add to it later and maybe use them in other templates. Thank you!  

---

