Symfony2 extends twig in bundle - no effect - php

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');

Related

Can I fliter out a Twig block from one page to another?

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

PHPUnit - mock twig tamplate

I am writing PHPUnit test and I am trying to test if my twig view is rendered correctly.
$expected='<html>
<p>The status of user!</p>;
</html>'
self::bootKernel();
$twig = self::$kernel->getContainer()->get('twig');
$actual = $twig->render('user-test.html.twig');
$this->assertEquals($expected, $actual);
But user-test.html.twig by default extends 'base.html.twig which is not needed in this test case.
Like:
{% extends 'base.html.twig' %}
<html>
<p>The status of user!</p>;
</html>
Is there a way I could mock base.html.twig template and test just user-test.html.twig without it throwing an error?
I think you have an error in your template because if you are extending another template all your HTML code needs to be in {% block blockname %}...{% endblock %} tags with blockname block defined in base.html.twig.
{# user-test.html.twig #}
{% extends 'base.html.twig' %}
{% block main %}
<html>
<p>The status of user!</p>;
</html>
{% endblock %}
Maybe what you can do is you can make another template with that part you need to check and include it.
{# user-test.html.twig #}
{% extends 'base.html.twig' %}
{% block main %}
{% include '_user-test.html.twig' %}
{% endblock %}
{# _user-test.html.twig #}
<html>
<p>The status of user!</p>;
</html>
And then test _user-test.html.twig that renders only what you want.

Symfony2/Twig - how to set template file contents as variable?

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

twig: can not override block in included file

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

Multiple extension of layout in twig/php

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

Categories