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.
Related
Hope someone can please advise.
I'm working on a theme for a CMS. I have one twig-based page as the theme template, so obviously all of the Twig blocks for the different types of pages are in the one document.
The CMS has two types of pages that can be created and viewed via the page builder.
At the moment, all of the Twig blocks appear in page A as well as page B from the font-end.
Is it possible for me to write some Twig code that will enable to me to display a Twig block on page A, but not on page B?
I understand I need some sort of filter, but do I filter by url, or is there a better way?
Appreciate any help you can give me.
To prevent a block from an extended template from showing you can override the block
main.twig
{% block foo %}
Foo
{% endblock %}
{% block bar %}
Bar
{% endblock %}
child.twig
{% extends "main.twig" %}
{% block foo %}{% endblock %}
The snippet above will just display Bar
To take it a step further, you can override a block and still let the original block of the extended template be executed, by using the function {{ parent() }}
child.twig
{% extends "main.twig" %}
{% block foo %}
Lorem Ipsum<br />
{{ parent() }}
{% endblock %}
This will output
Lorem Ipsum<br />
Foo
Bar
With this explained, you can wrap the function call parent() in any condition you like, thus controlling which block to show/hide
{% extends "main.twig" %}
{% block foo %}
{% if conditionA == true %}
{{ parent() }}
{% endif %}
{% endblock %}
{% block bar %}
{% if conditionB == true %}
{{ parent() }}
{% endif %}
{% endblock %}
demo
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 %}
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
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 %}