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