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.
From the doc :
Because use statements are resolved independently of the context passed to the template, the template reference cannot be an expression.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…