<p>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).</p>
<p>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 <a href="https://twig.symfony.com/doc/3.x/functions/include.html" rel="nofollow noreferrer">include</a> it wherever you need it. Something like this:</p>
<pre class="lang-twig prettyprint-override"><code>{# components/drink-list.twig #}

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

&lt;ul class=&quot;drinks&quot;&gt;
    {% for drink in drinks %}
        &lt;li class=&quot;drinks__item&quot;&gt;
            &lt;a href=&quot;{{ drink.url }}&quot; class=&quot;drinks__link&quot;&gt;
                {{- drink.title -}}
            &lt;/a&gt;
        &lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
</code></pre>
<p>Note that this template checks for the existance of two variables, <code>limit</code> and <code>drinks</code>, but provides reasonable defaults. Now you can include this template in other places to display lists of drinks:</p>
<pre class="lang-twig prettyprint-override"><code>{# 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(),
}) }}
</code></pre>
<p>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.</p>
<p>If you need even more flexibility, you can define <a href="https://twig.symfony.com/doc/3.x/tags/block.html" rel="nofollow noreferrer">blocks</a> that can be overwritten in <a href="https://twig.symfony.com/doc/3.x/tags/extends.html" rel="nofollow noreferrer">extending templates</a> or embeds.</p>
<hr />
<p>Other options for abstractions in Twig include:</p>
<ul>
<li>Twig templates with the <a href="https://twig.symfony.com/doc/3.x/tags/include.html" rel="nofollow noreferrer">include</a> or <a href="https://twig.symfony.com/doc/3.x/tags/embed.html" rel="nofollow noreferrer">embed</a> tag (see above).</li>
<li><a href="https://twig.symfony.com/doc/3.x/tags/macro.html" rel="nofollow noreferrer">Macros</a></li>
<li><a href="https://twig.symfony.com/doc/3.x/advanced.html#functions" rel="nofollow noreferrer">Functions</a>, <a href="https://twig.symfony.com/doc/3.x/advanced.html#filters" rel="nofollow noreferrer">filters</a> or <a href="https://twig.symfony.com/doc/3.x/advanced.html#tags" rel="nofollow noreferrer">tags</a> defined in a <a href="https://craftcms.com/docs/3.x/extend/extending-twig.html#register-a-twig-extension" rel="nofollow noreferrer">custom Twig extension</a></li>
<li><a href="https://twig.symfony.com/doc/3.x/templates.html#global-variables" rel="nofollow noreferrer">Global variables</a> provided through <a href="https://craftcms.com/docs/3.x/extend/extending-twig.html#extend-the-global-craft-variable" rel="nofollow noreferrer">hooks</a>.</li>
</ul>

CC BY-SA 4.0
2022-02-01T13:42:40.020Z
2022-02-01T13:49:59.057Z
2022-02-01T13:49:59.057Z
MoritzLost
<p>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 :-)</p>
<p>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:</p>
<pre><code>{% for drink in craft.entries.section('drinks').limit(5).all() %}
</code></pre>
<p>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:</p>
<pre><code>{% set drinkoverview = craft.entries.section('drinks').limit(5).all() %}
</code></pre>
<p>In that case the variable '<strong>drinkoverview</strong>' should be callable globally like:</p>
<pre><code>&lt;div&gt;{{ drinkoverview }}&lt;div&gt;
</code></pre>
<p>Maybe this call could be extended by parameters like the 'limit' somehow then?</p>
<pre><code>&lt;div&gt;{{ drinkoverview | limit-5 }}&lt;div&gt;
</code></pre>
<p>This would make the Craftbase even more dynamic and flexible in my opinion :-)</p>
<p>I hope anyone can answer my question here :-)
Thank you very much for reading and spending time thinking with me.</p>

<environmentvariables><custom>
Possibility to create / link custom (global) variables to shortcuts
# Possibility to create / link custom (global) variables to shortcuts

(Asked by: username_0 on 2022-02-01)

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:
{{ drinkoverview }}

Maybe this call could be extended by parameters like the 'limit' somehow then?
{{ drinkoverview | limit-5 }}

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.

**Tags:** environmentvariables, custom

---

## 1 answers

### Answer by username_1 on 2022-02-01. Score: 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()
%}


    {% for drink in drinks %}
        
            
                {{- drink.title -}}
            
        
    {% endfor %}


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:

Twig templates with the include or embed tag (see above).
Macros
Functions, filters or tags defined in a custom Twig extension
Global variables provided through hooks.


---

