PHPUnit - mock twig tamplate - php

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.

Related

Symfony TWIG override block content of embedded twig

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.

Symfony2 extends twig in bundle - no effect

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

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

How do i get all twig templates to inherit from master?

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

Categories