DEV Community

Discussion on: Emulating Components in Twig

Collapse
 
benrogerson profile image
Ben Rogerson • Edited

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 %}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jakedohm_34 profile image
Jake Dohm

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 😄

Collapse
 
benrogerson profile image
Ben Rogerson

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.