Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-24T23:43:28.720Z
Answers.Text: 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!
Answers.UserDisplayName: Katie Fritz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-24T22:29:10.747Z
Answers.LastActivityDate: 2016-08-24T22:29:10.747Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-24T22:17:09.140Z
Comments.Text: 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!
Comments.UserDisplayName: Katie Fritz
Tags: <templating><structure><filter><variable><index>
Title: Reusing logic to set string variables
ThreadText: # 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:
  
  ```
  <h2>Required Courses</h2>
  <ul>
    {% for entry in craft.entries.section('courses').type('safsCourse') %}
      <li><a href="{{ entry.url }}">{{ courseName }}</a></li>
    {% endfor %}
  </ul>
  
  <h2>Elective Courses</h2>
  
      <h3>At least one agricultural science course</h3>
      <ul>
        {% for entry in craft.entries.relatedTo(65) %}
          <li><a href="{{ msuLink }}">{{ courseName }}</a></li>
        {% endfor %}
      </ul>
  
      <h3>At least one social science course (3cr)</h3>
      <ul>
        {% for entry in craft.entries.relatedTo(66) %}
          <li>a href="{{ msuLink }}">{{ courseName }}</a></li>
        {% endfor %}
      </ul>
  
  ```
  
  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](http://twig.sensiolabs.org/doc/tags/macro.html). 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 %}
  
  <h2>Required Courses</h2>
  <ul>
      {% for entry in craft.entries.section('courses').type('safsCourse') %}
          <li><a href="{{ entry.url }}">{{ helpers.courseName(entry) }}</a></li>
      {% endfor %}
  </ul>
  
  <h2>Elective Courses</h2>
  
  <h3>At least one agricultural science course</h3>
  <ul>
      {% for entry in craft.entries.relatedTo(65) %}
          <li><a href="{{ helpers.msuLink(entry) }}">{{ helpers.courseName(entry) }}</a></li>
      {% endfor %}
  </ul>
  
  ...
  
  ```
  
  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!  
  
  ---
  
  