How can I override a block inside an included template file?
example:
{# layout.html #}
{% include "menu.html" %}
{# menu.html #}
{% block overrideme %}{% endblock %}
{# index.html #}
{% extends "layout.html" %}
{% block overrideme %}Overriden{% endblock %}
I read somewhere that a trait function was implemented? I can't find any documentation about it though, does anyone know how I could make this work?
If you want to override blocks inside a file that you are including then you should 'embed' it rather than 'include' it.
{% embed "menu.html" %}
{% block overrideme %}
Overriden
{% endblock %}
{% endembed %}
See the docs for more details: http://twig.sensiolabs.org/doc/tags/embed.html
Related
In Symfony 2.8 I've got SharedBundle, where I keep views and other things I share between other bundles, including the whole layout that should be extended by other bundles' views.
That shared layout has some typical blocks, from which sidebar block should be different for different bundles.
Currently I did an ugly workaround:
In SharedBundle sidebar container:
{% if sidebarcontent is defined %}
{{ sidebarcontent|raw }}
{% else %}
SIDEBAR NOT FOUND
{% endif %}
And in other bundles (here: BundleA) in every view that extends the shared main view:
{% extends 'SharedBundle:layout.html.twig' %}
{% block sidebar %}
{% include 'BundleA:_partials:sidebar.html.twig' %}
{% endblock %}
{% set sidebarcontent = block('sidebar') %}
But that doesn't look good, I think. Is there a better way?
I think this is a valid approach. The only simplification I can think of is not using the variable sidebarcontent.
You can use block('sidebar') inside the if statement:
{% if block('sidebar')|length > 0 %}
{{ sidebarcontent|raw }}
{% else %}
SIDEBAR NOT FOUND
{% endif %}
Make sure the block exists before checking it's content, so initialise it with an empty string:
{% block sidebar %}{% endblock %}
I have some twigs that are embedded in base twig. Embedded twigs contain blocks that I'd like to override in other twigs, which extend base. This changes are not showing. I saw similar questions, but couldn't deduce the answer from that.
For example in base twig:
<body>
<div id="wrapper">
{% embed 'Bundle::sidebar.html.twig' %}{% endembed %}
</div>
</body>
Sidebar twig contains the block that should be overridden:
<div>Some content here</div>
{% block example_block %}
Content of a block
{% endblock %}
Twig that extends the base:
{% extends 'Bundle::base.html.twig' %}
{% block example_block %}
I want different content here
{% endblock %}
Based on the Docs on embed http://twig.sensiolabs.org/doc/tags/embed.html I think this should work…
Base Twig Template:
<body>
<div id="wrapper">
{% block sidebar %}
{% embed 'Bundle::sidebar.html.twig' %}{% endembed %}
{% endblock %}
</div>
</body>
Twig that extends base:
{% extends 'Bundle::base.html.twig' %}
{% block sidebar %}
{% embed "Bundle::sidebar.html.twig" %}
{# This block is defined in "sidebar.html.twig" #}
{# and we override it right here: #}
{% block example_block %}
I want different content here
{% endblock %}
{% endembed %}
{% endblock %}
If you declare a sidebar block in the base template, then override it in the extended file, declaring the embed again and the blocks you want to override.
I do my first project in Symfony2. I have problem with template. My main twig file is "index.html.twig". It's located in src/Gogs/CMSBundle/Resources/views/Default/index.html.twig
I try to extends main twig file from content.html.twig -> it's also in the same directory.
In index.html.twig I have {% block body %}{% endblock %}.
My content.html.twig looks like:
{% extends 'GogsCMSBundle:Default:index.html.twig' %}
{% block body %}
Some content
{% endblock %}
I was looking solution on many forums, but nothing working. Symfony doesn't give me any errors - no effect.
When I try to use include in index.html.twig , it's work perfectly. Code below.
{% include "GogsCMSBundle:Default:content.html.twig" %}
I tried to use also another commands:
{% extends 'GogsCMSBundle:Default:index.html.twig' %}
{% extends 'GogsCMSBundle::index.html.twig' %}
{% extends 'GogsCMSBundle:index.html.twig' %}
{% extends '::index.html.twig' %}
{% extends 'index.html.twig' %}
All of them doesn't give any effect - no errors too.
My main controller:
return $this->render('GogsCMSBundle:Default:index.html.twig', array('name' => $page, 'menu' => $menu));
I cleaned Cache many times.
You need move indexd.html.twig in app\Resources\views = this is a base layout of the symfony. Next create in src/YourBundle/Resources/views layout.html.twig -which extend index.html.twig:
{% extends '::index.html.twig' %}
And finally, add in content.html.twig :
{% extends 'GogsCMSBundle::layout.html.twig' %}
More information there
#Artamiel found the answer...
Bug is in Controller. Contoller must indicate a content.html.twig.
So solution:
index.html.twig
{# src/Gogs/CMSBundle/Resources/views/Default/index.html.twig #}
{% block body %}{% endblock %}.
content.html.twig
{# src/Gogs/CMSBundle/Resources/views/Default/content.html.twig #}
{% extends 'GogsCMSBundle:Default:index.html.twig' %}
{% block body %}
Some content
{% endblock %}
Contoller.php
return $this->render('GogsCMSBundle:Default:content.html.twig');
I have a problem figuring out how to solve the following issue with Twig templates
I have a system where two parts wants to change blocks in the layout without know each other. The idea of my templates are
main.twig
<html><body>
{% block a %}{% endblock %}
{% block b %}{% endblock %}
</body></html>
replacea.twig
{% extends "main.twig" %}
{% block a %}hello{% endblock %}
replaceb.twig
{% extends "main.twig" %}
{% block b %}world{% endblock %}
My problem is that I do not know how to achieve this, as the places where I call replacea and replaceb only shares a "viewengine" so I am able to collect all render calls and bulk them.
My first idea to solve this was to extend a variable, say "layout", but then when I call render layout would be replaced with the same in all templates and not with "replacea.twig" in replaceb and with "main.twig" in replacea.
Hope you understand my problem.
You need to use "Use" - pun not intended! :-)
main.twig
<html><body>
{% block a %}{% endblock %}
{% block b %}{% endblock %}
</body></html>
replacea.twig
{% block a %}hello{% endblock %}
replaceb.twig
{% extends "main.twig" %}
{% use "replacea.html" %}
{% block b %}world{% endblock %}
Check out the documentation:
https://twig.symfony.com/doc/tags/use.html
NB: i´m using Twig in a non-Symfony context.
I want to register a master layout that all templates should inherit, so that i don´t have to forget to preface them with:
{% extends 'layout.html.twig' %}
I know i have seen this somewhere, and Symfony makes use of it.
It is possible!
After some experimenting i came up with this solution:
{# index.html.twig #}
{% block title %}Hello world{% endblock%}
Wrap global layout in a block:
{# layout.html.twig #}
{% block layout %}
<html>
<head>
<title>{% block title %}{% endblock%}</title>
</head>
<html>
{% endblock %}
Pass child template to twig:
// index.php
...
$twig->display(array('template'=>'index.html.twig'));
...
Inject child template via a proxy template:
{# proxy.twig #}
{% extends 'layout.html.twig' %}
{% block layout %}
{# Get extended block #}
{{ parent() }}
{# inject template into master layout #}
{% include template %}
{% endblock %}