第二个父 layout.twig.html symfony2 中的调用块

call block in second parent layout.twig.html symfony2

我想在用户输入此页面时执行 javascript:page1.twig.html

    {% extends "Test1Bundle::layout1.html.twig" %}

   // block code javascript here

    {% block content %}
    <div>
        <span>Alexa .</span>
    </div>
    {% endblock %}

layout1.html.twig :

{% extends 'Test1Bundle::layout2.html.twig' %}
<p>
My name is : {% block content %}{% content %}
</p>

layout2.html.twig :

<!DOCTYPE html>
<html lang="fr">
<head>
// call block javascript here
</head>
.
.
.

我不知道我为此做了什么以及该使用哪个功能!请帮忙!

您需要在 parents 中定义空块,您希望在其中查看您的内容:

page1.html.twig:

{% extends "layout1.html.twig" %}

{% block script %}

 <script type="text/javascript">
   // whatever
 </script>

{% endblock %}

{% block name %}
<div>
    <span>Alexa .</span>
</div>
{% endblock %}

layout1.html.twig:

{% extends 'layout2.html.twig' %}
{% block content %}
<p>
My name is : {% block name %}{% endblock %}
</p>
{% endblock %}

layout2.html.twig:

<!DOCTYPE html>
<html lang="fr">
<head>
   {# btw, scripts are better at the bottom of the body #}
   {% block script %}{% endblock %}
</head>
<body>
   {% block content %}{% endblock %}
</body>
</html>

fiddle