user3178734
user3178734

Reputation: 73

Including block content between templates

I have a big problem with including blocks between templates. The code is following:

{# 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 %}

The idea is, index.html.twig extends layout, and rendering another template with name slideshow.html.twig trought controller. But I need include stylesheets inner block in the template slideshow.html.twig to stylesheets block in head position in the main template layout.html.twig, but does not work. When i add tag {{ parent() }} to the block stylesheet in the template slideshow.html.twig, symfony say

Calling "parent" on a template that does not extend nor "use" another template is forbidden in demoBundle:Gallery:slideshow.html.twig at line 3

/EDITED*/

No, it is not resolution for me, because i have some variables returnet from defaultcontroller.php in template index.html.twig.

Ok, the previous code was just a simpliefed example. I enclose a concrete example

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 is for example "slideshow". In the DefaultController.php inner method renderIndexBlocksAction is this piece of code:

DefaultController.php

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

this render template name slideshow.html.twig, there is this piece of code:

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 %}

for completenes, her is the piece of 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>

Upvotes: 0

Views: 2290

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

The error itself says calling parent on a template that does not extend nor use means your template slideshow.html.twig is not extending any parent template or layout to follow thus if there is not parent template you cannot call the stylesheets of member that does not exist,for answer to your question in slideshow.html.twig use

{% extends '::index.html.twig' %}

and then define

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

If there are any stylesheets in index.html.twig these will be included in your template,as viewing the index.html.twig code there is empty stylesheets block.If layout.html.twig has also defined a block for stylesheets then you should call {{ parent() }} function in the stylesheets block of index.html.twig so it will include the stylesheets of its parent layout i.e layout.html.twig

Upvotes: 1

Related Questions