twig-tips

Make your include an embed, if needed.

Right before launch, the client request a one-off new card layout that re-uses the card styling, but has different contents.

Instead of duplicating the card component and changing the contents, you can use your include as an embed where needed.

{# _news/index.twig #}

{% for entry in craft.entries({
    section: "news",
    limit: 10,
}).all() %}
    <div>
        {% embed "_components/card" only %}
            {% block main %}
                One-off card content goes here.
            {% endblock %}
        {% endembed %}
    </div>
{% endfor %}
{# _components/card.twig #}

{% set image = image ?? null %}
{% set heading = heading ?? "Lorem ipsum dolor sit amet" %}
{% set description = description ?? null %}

<div>
    {% block main %}
        {% if image %}
            <img src="{{ image }}" alt="">
        {% endif %}
        <h3>{{ heading }}</h3>
        {{ description }}
    {% endblock %}
</div>