包括模板之间的块内容

时间:2014-03-05 20:52:35

标签: javascript symfony styles twig block

我在模板之间包含块有一个很大的问题。代码如下:

{# layout.html.twig #} (simplified version)
<html>
<head>
.....
.....
{% block stylesheets%}{% endblock %}
{% block javascripts %}{% endblock %}
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>

{# index.html.twig #}
{% extends "demoBundle::layout.html.twig" %}
{% block content %}
some content           {# <---- this is working #}
{% render(controller('demoBundle:Default:renderIndexContent')) %}
{% endblock %}

{# DefaultController.php #}
...
...
return $this->render('demoBundle:Gallery:slideshow.html.twig');
...
...

{# slideshow.html.twig #}
{% block stylesheet %}   {# <---- this is not working #}
<link rel="stylesheets" href="{{ asset('bundles/cms/css/pictureSlider.css') }}"/>
{% endblock %}

这个想法是,index.html.twig扩展布局,并使用名称slideshow.html.twig trought controller渲染另一个模板。但我需要在模板slideshow.html.twig中将样式表内部块包含在主模板layout.html.twig中头部位置的样式表块中,但不起作用。当我将标签{{parent()}}添加到模板slideshow.html.twig中的块样式表时,symfony说

在不扩展或“使用”另一个模板的模板上调用“父”在demoBundle中禁止:Gallery:slideshow.html.twig at line 3

/ EDITED * /

不,这对我来说不是解决方案,因为我在模板index.html.twig中有一些来自defaultcontroller.php的变量。

好的,之前的代码只是一个简化的例子。我附上一个具体的例子

index.html.twig     {%extends“cmsBundle :: layout.html.twig”%}

{% block rightSideBar %}{% endblock %}

{% block content %}
    <div class="top_block">
      <div id="module-1" class="no-title">
          {% for block in blocksCenter %}
            {% if (block.position=="center-top") %}
                {% render(controller('cmsBundle:Default:renderIndexBlocks',{'blockname':block.name})) %}
            {% endif %}
          {% endfor %}
      </div>

...

block.name例如是“幻灯片放映”。在DefaultController.php内部方法中,renderIndexBlocksAction就是这段代码:

DefaultController.php

return $this->render(
                'cmsBundle:Blocks:'.$blockname.'.html.twig'
                ,array('items'=>$items)
                );

这个渲染模板名称为slideshow.html.twig,有这段代码:

slideshow.html.twig

{% block styles %}
{{ parent() }}
    <link rel="stylesheet" href="{{ asset('bundles/cms/css/pictureSlider.css') }}" type="text/css" media="all"/>

{% endblock %}
{% block javascripts %}
    <script type="text/javascript" src="{{ asset('bundles/cms/js/jquery-1.11.0.min.js') }}"></script>
{% endblock %}

对于completenes,她是layout.html.twig

layout.html.twig

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=8">
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <link rel="shortcut icon" href="{{ asset('bundles/cms/images/favicon.ico') }}" type="image/x-icon" />
    <meta name="keywords" content="" />
    <meta name="robots" content="index, follow" />
    <link rel="stylesheet" href="{{ asset('bundles/cms/css/style.css') }}" type="text/css" media="all"/>
    <link rel="stylesheet" href="{{ asset('bundles/cms/css/menu.css') }}" type="text/css" media="all"/>
    {% block styles %}{% endblock %}
    {% block javascripts %}{% endblock %}
</head>
<body>
    <div id="bg_image"><img src="{{ asset('bundles/cms/images/AbstractBlue.jpg') }}" alt="" style="width:100%; height:100%;"></div>
    <div id="mainContainer">
        <div id="header">
            <div id="top1">
                <div id="logo">
                    <div id="search">
                        {% include 'cmsBundle:Default:search.html.twig' %}
                    </div>
                    <div class="wrapper" style="width:987px"/>
                            <div id="menu" class="menu">
                                {% render(controller('cmsBundle:Default:renderMenu')) %}
                            </div>
                        </div>
                    </div>
                </div>
        </div>

        <div id="blocks" class="blocks">{% block rightSideBar %}{% endblock %}</div>

        <!--CONTENT-->
            <div id="mainContentm">
                {% block content %}{% endblock %}
            </div><!-- end #content -->

            <!-- #footer -->
            <div id="footer">
                <div id="footer-left">
                </div>

                <div id="footer-right">
                </div>
            </div><!-- end #footer -->

    </div><!-- end #container -->
</body>
</html>

1 个答案:

答案 0 :(得分:1)

错误本身表示在未扩展或未使用的模板上调用父级意味着您的模板slideshow.html.twig未扩展任何父模板或布局,因此如果没有父模板则无法调用成员的样式表在slideshow.html.twig使用

中回答您的问题时不存在
{% extends '::index.html.twig' %}

然后定义

{% block stylesheets %}
{{ parent() }}
<link rel="stylesheets" href="{{ asset('bundles/cms/css/pictureSlider.css') }}"/>
{% endblock %}

如果index.html.twig中有任何样式表,那么这些样式表将包含在您的模板中,因为查看index.html.twig代码时会有空stylesheets块。如果layout.html.twig也定义了样式表的块然后你应该在{{ parent() }}的样式表块中调用index.html.twig函数,这样它将包含其父布局的样式表,即layout.html.twig