2

I'm trying to use a macro to output a list of entries (and the title of a section), this is my code:

{% macro sections(sectionName)%}

    {% set index = craft.entries.section('{{ sectionName }}Index').first %}
    <h3>{{ index.getLink() }}</h3>

    {% for entry in craft.entries.section('{{ sectionName }}').order('title') %}

        {{ loop.first ? '<ul>'}}
            <li><a href="{{ entry.url }}">{{ entry.title }}</a> {{ sectionName }}</li>
        {{ loop.last ? '</ul>'}}

    {% endfor %}
{% endmacro %}

{{ _self.sections('about') }}
{{ _self.sections('information') }}
{{ _self.sections('galleries') }}

But this displays ALL entries. However, if I hardcode the section names (for each loop) it works, though not DRY.

This is my first attempt at using a macro, seemed like a good place to use one?

1 Answer 1

3

You don't want braces within braces.

You want something like this instead:

{% set index = craft.entries.section(sectionName ~ 'Index').first %}

Same needs changing in the for loop.

1
  • Gah! Of course! Thank you. And for the heads up on the ~
    – Matt Ellis
    Commented Jan 6, 2016 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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