4

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?

  • 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 Aug 24 '16 at 22:17
4

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 %}

<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).

| improve this answer | |
  • 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 Aug 24 '16 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.