1

Maybe I'm a bit vague in the topic-title, but here's a more in depth explanation of my question. I'm still a total n00b when it comes to Craft / Templating, so I hope you would forgive me if I'm asking 'dumb' questions :-)

At the moment of writing, I get all things done, but I wandered if there would be a way to make things even a bit easier in some ways. Let's say that I want to do a FOR loop like:

{% for drink in craft.entries.section('drinks').limit(5).all() %}

Is there a way to shorten the above example by assigning it to an easier to remember shortcut? Maybe in a separate file. Something like this approach:

{% set drinkoverview = craft.entries.section('drinks').limit(5).all() %}

In that case the variable 'drinkoverview' should be callable globally like:

<div>{{ drinkoverview }}<div>

Maybe this call could be extended by parameters like the 'limit' somehow then?

<div>{{ drinkoverview | limit-5 }}<div>

This would make the Craftbase even more dynamic and flexible in my opinion :-)

I hope anyone can answer my question here :-) Thank you very much for reading and spending time thinking with me.

1 Answer 1

1

There's no blanket solution for this, there are many options to providing reusable snippets, variables etc. As a rule of thumb, only use abstractions like this if you're gonna need them in more than one place (some say at least 3).

In this case, it looks like you're trying to create an abstraction for a template that displays a list of beverages, right? In this case, I'd put that template in a separate Twig file and include it wherever you need it. Something like this:

{# components/drink-list.twig #}

{% set limit = limit ?? 5 %}
{% set drinks = drinks ?? craft.entries()
    .section('drinks')
    .limit(limit)
    .orderBy('postDate DESC')
    .all()
%}

<ul class="drinks">
    {% for drink in drinks %}
        <li class="drinks__item">
            <a href="{{ drink.url }}" class="drinks__link">
                {{- drink.title -}}
            </a>
        </li>
    {% endfor %}
</ul>

Note that this template checks for the existance of two variables, limit and drinks, but provides reasonable defaults. Now you can include this template in other places to display lists of drinks:

{# Display the five newest drinks #}
{{ include('components/drinks') }}

{# Display the ten newest drinks #}
{{ include('components/drinks', {
    limit: 10,
}) }}

{# Display a list of whisky drinks #}
{{ include('components/drinks', {
    drinks: craft.entries().section('drinks').category('whisky').all(),
}) }}

Now you have the display logic for your 'list of drinks' template in a central place and can change it whenever you like. You can add additional parameters to make this template more flexible as you go.

If you need even more flexibility, you can define blocks that can be overwritten in extending templates or embeds.


Other options for abstractions in Twig include:

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.