Background:
One of my templates has up to 6 fields that should each go into the same markup. Here's an example with a courseDesign
field:
{% if entry.courseDesign|length %}
<section class="col-xl mb-3">
<h2 id="courseDesign">Course Design</h2>
{{ entry.courseDesign }}
</section>
{% endif %}
This worked fine when I wrote each section out like this. However, once I also added a table of contents/nav, I decided to refactor.
I now have a courseFields
array, and I'm adding each field to it if it exists:
{% set courseFields = {} %}
{% if entry.courseDesign %}
{% set courseFields = courseFields|merge({ "courseDesign": { handle: "courseDesign", title: "Course Design" } }) %}
{% endif %}
My navigation macro works perfectly, because it's only interacting with the objects in the courseFields
array.
My problem:
I can't figure out how to iterate through my fields and get the actual content from them using a macro.
If I use {{ entry.handle }} in my macro, even when handle
is a valid field handle, I get the runtime error Variable "entry" does not exist.
I also tried:
{% set courseField = "entry." ~handle %}
{{ courseField }}
but that produces a string: "entry.courseDesign".
How do I access entry
from my macro? Should I be going about this a different way?
attribute()
function? I'm not sure how to use it.