好的,我不确定我是否在这里错过了一个技巧。但是我在文档或在线解决方案中找不到任何内容。基本上我试图在模板块中包含一大块javascript,但样式也是如此。我总结了模板结构:
{# base.html.twig %}
<html>
  <head>
    <title>{% block title %}Base template{% endblock %}</title>
    {% block stylesheets %}
      <link rel="stylesheet" href="{{ asset('bundles/thisBundle/css/theme.css') }}" type="text/css" media="all" />
    {% endblock %}
    {% block scripts %}{% endblock %}
  <head>
  <body>
    {% block content %}hello from base{% endblock %}
    {% block javascripts %}
    {% endblock %}
  </body>
</html>
{# index.html.twig #}
{% extends 'base.html.twig %}
{% block content %}
  {% include 'ThisBundleBundle::templatewithascript.html.twig' %}
  <p>Some content here<p>
  {% include 'ThisBundleBundle::anothertemplatewithascript.html.twig' %}
{% endblock %}
现在这是我的问题。从这个模板我想添加到 javascripts/stylesheets 块的内容,但我的包含在内容块内,我不能在 templatewithascript.html.twig 中扩展 index.html.twig,基本上我希望这个工作:
{# templatewithascript.html.twig #}
{% block stylesheets %}
  <link href="{{ asset('bundles/thisBundle/css/blockspecificstyle.css') }}" rel="stylesheet" type="text/css" />
{% endblock %}
{% block body %}
  <p>Click here and something happens</p>
{% endblock %}
{% block javascripts %}
  <script type="text/javascript">
    $(document).ready(function(){
      $("p").click(function(){
        $(this).hide();
      });
    });
  </script>
{% endblock %}
或者这只是一个限制,我必须在包含内容的模板中包含样式/脚本?谢谢。鲁