Bramastic February 2016

Twig template parent() caused double parent block

I have a base.html.twig template.html.twig and dashboard.html.twig. Dashboard extends template which extends base.

Base:

{% block javascripts %} 
    <script src="1.js"></script> 
{% endblock %}

Template:

{% block javascripts %} 
    {{ parent() }}
    <script src="2.js"></script>
{% endblock %}

Dashboard:

{% block javascripts %}
    {{ parent() }}
    <script src="3.js"></script>
{% endblock %}

This templating setup resulted in redundant script tags where everything is doubled like this

<script src="1.js"></script>
<script src="2.js"></script>
<script src="3.js"></script>
<script src="1.js"></script>
<script src="2.js"></script>
<script src="3.js"></script>

Am I missing something? Why is it happening?

Edit1:

base.html.twig:

extends nothing

template.html.twig:

{% extends 'base.html.twig' %}

default/dashboard.html.twig:

{% extends '::template.html.twig' %}

Answers


n1c01a5 February 2016

In my opinion your {% extends "file" %} is bad.

The fix :

Template :

{% extends "base_path" %}
{% block javascripts %} 
    {{ parent() }}
    <script src="2.js"></script>
{% endblock %}

Dashboard :

{% extends "template_path" %}
{% block javascripts %}
    {{ parent() }}
    <script src="3.js"></script>
{% endblock %}

Post Status

Asked in February 2016
Viewed 2,636 times
Voted 4
Answered 1 times

Search




Leave an answer