re: Emulating Components in Twig VIEW POST


I love using embeds, but there's another drawback you should be aware of:

When using any macros within those blocks you need to add the import within the embed.
If you don't then you get a real obscure error that doesn't leave many clues as to what's going on.

{# Import as normal #}
{% import 'macros/index.twig' as macro %}
{{ macro.makeMeASandwich }}

{% embed "page.twig" with {} %}
    {# Have to import again as the import above isn't in scope here #}
    {% import 'macros/index.twig' as macro %}

    {% block main %}
        <div>{{ macro.makeMeASandwich }}</div>
    {% endblock %}

{% endembed %}

Thanks for mentioning this!

I actually like having to explicitly import embeds, so it's not a problem for me, but it's definitely a good thing to note 😄


No problem!

I'd enjoy twig imports more if they acted like Javascript imports.

In this instance you're forced away from the common practice of declaring them all at the top of the page and instead be within the embed tag.

No deal breakers though, embeds are great.

code of conduct - report abuse