xfloys2112
xfloys2112

Reputation: 685

Twig combine multiple blocks using USE

I have a problem and I'm trying to see if anyone has a solution for it.

I have a twig template that extends base:

{% extends "base.html" %}

{% use "element1.html" %}
{% use "element2.html" %}

{% block css %}
{{ parent() }}
{% endblock %}

{% block body %}{% endblock %}

{% block javascript %}
{{ parent() }}
{% endblock %}

element1.html and element2.html are almost the same

{# element1.html #}

{% block css %}
some css...
{% endblock %}

{% block body %}
some body html    
{% endblock %}

{% block javascript %}
some javascript...
{% endblock %}

When the code runs, element2 overwrites element1, Is there a way to combine blocks, just like parent() combines base blocked with the main template?

Upvotes: 0

Views: 815

Answers (1)

satdev86
satdev86

Reputation: 810

Please name the blocks of element1 and element2 appropriately so that it does not override the other.

{% use "element1.html" with css as element1_css, body as element1_body, javascript as element1_js %}
{% use "element2.html" with css as element2_css, body as element2_body, javascript as element2_js %}

And now use appropriate blocks from element1 or element2 such as

{% block element1_css %}{% endblock element1_css %}

Upvotes: 2

Related Questions