I'd like to include a widget in different pages and have the related scripts, defined in this sub-template, happened automatically at the end of the page with the other scripts without having to do several includes.
I tried to use a block
in an included sub-template and add some script in that block defined itself in base.html.twig so those scripts would load at the end of the page.
In base.html.twig I have
<html><body>
<div>{% block content %}{% endblock %}</div>
<aside>{% block aside %}{% endblock %}</aside>
...
{% block javascripts %}
<script src="..."></script>
<script src="..."></script>
...
{% endblock %}
{% block widgetInlineScript %}{% endblock %}
</body></html>
I have a template profile.html.twig which extends base.html.twig and looks so
{% extends '::base.html.twig' %}
{% block content %}
...
{% endblock %}
{% block aside %}{% include 'ApplicationRcBundle:include:promo/adSpace_topRight.html.twig' %}{% endblock %}
In include/widget.html.twig I couldn't use {% block javascripts %}{{ parent() }} ...
because it doesn't extend anything itself so using parent()
fail. Instead I used a specific block.
{% block widgetInlineScript %}
<script type="text/javascript" id="widgetInlineScript" >
...
</script>
{% endblock %}
Twig compile the template well but render the script in the DOM just after the widget instead of rendering it in place of {% block widgetInlineScript %}
at the end of the page.
So is it possible to use a block
inside an included subtemplate ?
Thanks
widgetInlineScript
(like you did for theaside
block). – Thomas Potaire