0
votes

is there any way to call parent() function in extending block in the template of the custom extension?

In my base.html.twig I have block footer_javascript and in my custom extension I would like to add some JS to this block but I need to ensure that original block footer_javascript will be extended not overwritten. I tried this:

custom_extension.html.twig:

{# some html/twig code, not really important #}
{% block footer_javascript %}
    {{ parent() }}
    {# some javascript for this custom twig extension #}
{% endblock footer_javascript %}

but of course, I get

Calling "parent" on a template that does not extend nor "use" another template is forbidden

base.html.twig - base template with block structure:

{# base html/twig structure, not really important %}

{# block which will be extended/overwriten in templates which extends this base.html.twig #}
{% block footer_javascript %}
{% endblock footer_javascript %}

extended.html.twig - template which extends base.html.twig; in this template I am using custom twig extension:

{% extends "::base.html.twig" %}

{# some html/twig ... #}

{{ custom_extension(entity) }}

{% block footer_javascript %}
{{ parent() }}
{# javascript used for extend.html.twig #}
{% endblock footer_javascript %}

Is there any way to extend base footer_javascript in my custom extension template?

1
Can you show your template as well as your base.html.twig?Bartek
@bartek I added some outline for base.html.twig, but the other code is not really important. If you have some specific question just ask and I'll try to answer.Cockootec

1 Answers

3
votes

To use parent(), you have to extend the template.

Your custom_extension.html.twig has to extend base.html.twig like extended.html.twig.

EDIT:
If you're using 2 templates with <body>, you don't have to use {{ parent() }}, but instead put your js in a twig that you will include in your block.