1

I'm trying to achieve some modularity using embeds, but with the possiblity to extend/inject customizations from the including template.

Consider the following scenario:

Have a modular partial which contains a form, say form.twig I want the ability to add new form fields at the end of the form, but using the otherwise common form.twig, so I add a {% block form_extension %} in form.twig

I have another page template, that includes/embed that partial, say page.twig. Now, this works, I just add a block when embedding, and it gets injected.

But, I also have another modular partial, say partial_with_form.twig, that embeds form.twig. And I want to be able to define the form_extension block, when embedding partial_with_form.

Is this even possible? I have tried with parent() and block(), but none of them can give me the content of the supplied block unless I specify the targeting block in partial_with_form. But then it will be outputted there...

2

If partial_with_form.twig isn’t making any of its own customizations to form.twig, then I’d just include the form.twig template using an {% include %} tag, and wrap that whole thing with a {% block %}, so if a template wants to include the partial and make changes to the form, they can just redefine the form themselves.

partial_with_form.twig:

{% block form %}
  {% include 'form' %}
{% endblock %}

Other template:

{% embed `partial_with_form` %}
  {% block form %}
    {% embed 'form' %}
      {% block form_extension %}
        <!-- ... -->
      {% endblock %}
    {% endembed %}
  {% endblock %}
{% endembed %}
|improve this answer|||||
  • Yeah, that crossed my mind later on as well, maybe that is actually easier in the long term and less confusing. – jishi Feb 21 at 13:14
1

Hm, seems like you can use block() but it can't be within the embed block. Putting it in a variable first and use that variable in the embed block works.

{% set petition_after_fields_block = block('petition_after_fields') is defined ? block('petition_after_fields') %}
{% embed 'form' %}
    {% block petition_after_fields %}
        {{ petition_after_fields_block|raw }}
        {# {{ block('petition_after_fields') }} -- This doesn't work, nor using a different block name #}
    {% endblock %}
{% endembed %}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.