Reputation: 451
I would like to inject inside the styles and scripts blocks in my layout new values, but from the embed block.
Of course it throws the error Calling "parent" outside a block is forbidden.
.
Is there any workaround ?
layout.html.twig:
<!DOCTYPE html>
<html>
<head>
{% block style %}
<link rel="stylesheet" href="foo.css">
{% endblock %}
</head>
<body>
{% block content "" %}
{% block scripts %}
<script src="foo.js"></script>
{% endblock %}
</body>
</html>
list.html.twig:
{% extends 'layout.html.twig' %}
{% block content %}
{% embed datatable.html.twig %}
{% block tbody %}
<tr>
<td>my awesome table</td>
</tr>
{% endblock %}
{% endembed %}
{% endblock %}
datatable.html.twig:
<table id="myDatatable">
<tbody>
{% block tbody "" %}
</tbody>
</table>
{% block styles %}
{{ parent() }}
<link rel="stylesheet" href="dataTables.css">
{% endblock %}
{% block scripts %}
{{ parent() }}
<script src="dataTables.js"></script>
{% endblock %}
(And I cant/wont use the blocks scripts
and styles
inside the list.html.twig
. They are part of the datatable template, it will not make any sens to define theme in the list.html.twig
.).
And sadly I cant use use
because this function does not support dynamics properties, only strings.
Because use statements are resolved independently of the context passed to the template, the template reference cannot be an expression.
Upvotes: 3
Views: 1587
Reputation: 15649
As said in the comment, includes/embeds can't alter blocks from their includer. That said there is an extension available that could solve your problem.
This Deferred Twig Extension
can be found here
Basically the node postpones the execution of a said block. This way you can create a variable that holds all of your javascript links and output them. This can be seen in the advanced example found on the github.
credits to Eugene Leonovich for making this extension
Upvotes: 2