Symfony2: Custom Error Page Extend base.html.twig - symfony

I am trying to customize the error pages in Symfony.
This is my error.html.twig file located in app/Resources/TwigBundle/views/Exception/:
{% extends '::base.html.twig' %}
{% block body %}
<h1>{{ status_code }}: {{ status_text }}</h1>
{% endblock %}
Unfortunately I get the following error message:
Fatal error: Uncaught exception
'Symfony\Component\Routing\Exception\ResourceNotFoundException' in
[...]
vendor\symfony\symfony\src\Symfony\Component\HttpKernel\EventListener\RouterListener.php
on line 144
When I remove {% extends '::base.html.twig' %} everything works fine. Any ideas how to have my base template included in the error page?
Edit 1:
The strange thing is that it seems to work when a 403 is thrown, e.g., when I access /user but don't have the necessary privilege.
Edit 2:
I pasted the whole content of my base.html.twig into the error.html.twig file and noticed that the error was caused due to the menu rendered by the KnpMenuBundle bundle:
{{ knp_menu_render('ACMEMemberBundle:Builder:mainMenu', { 'style': 'pills', 'currentClass': 'active' }) }}
When I remove this line, everything works fine. But this is not the way I would like to go. Is there no possibility to keep the navigation?

file should be located in app/Resources/views/Exception/
instead of
app/Resources/TwigBundle/views/Exception/

Did you put the page in the following place?
/app/ResourceS/TwigBundle/views/Exception/error404.html.twig
{% extends '::base.html.twig' %}
{% block content %}
{%trans%}errors.404{%endtrans%}
{% endblock %}

// app/Resouces/views/base.html.twig
×
{% include('path/to/include') %}
○
{% include('::path/to/include') %}

I finally have done it putting the whole code (base+current) in the same 'error.html.twig' file.
It works for me and avoided a huge headache.

plz remove :: in first line https://symfony.com/doc/current/templating.html
{% extends 'base.html.twig' %}
{% block body %}
<h1>{{ status_code }}: {{ status_text }}</h1>
{% endblock %}

Related

Including header in base.twig for all pages

It is something pretty simple to be done outside of twig but here I am not even sure it is possible. This is the case:
Have base.html.twig. header.html.twig, home.html.twig.
#home.html.twig
{% extends 'base.html.twig' %}
{% block body %}
<h1>MY HTML</h1>
{% endblock %}
#base.html.twig
....some html here
{% block header%}{% endblock %}
{% block body %}{% endblock %}
....some more html here
#header.html.twig
{% extends 'base.html.twig' %}
{% block header %}
some here things that have to shown on every page through base.html.twig
{% endblock %}
I think it is pretty straight forward scenario but still my header doesn't show anywhere. As I understood from the documentation it is not working because this is how blocks work. It renders the page i am calling from my controller (home.html.twig) and the extended by it (base.html.twig). But wont call the header as well. So! How should I call the header on every page ?
To add the header on all pages just put include see example
{% block header %}
{% include 'header.html.twig' %}
{% endblock %}

Symfony-twig template form theme error

I have everything working correctly and now I'm trying to work with form themes. This is my code to generate the form without a theme.
{% extends 'base.html.twig' %}
{% block body %}
{% include 'menu/menu.html.twig' %}
{% if addpost is defined %}
<div id='add_post_form'>
{{ form_start(addpost) }}
{{ form_widget(addpost) }}
{{ form_end(addpost) }}
</div>
{% endif %}
{% endblock %}
But when I'm adding a form-theme with the following code
{% form_theme form 'form/form_div_layout.html.twig' %}
I get this error:
Variable "form" does not exist
When i execute this without the line, I'm getting the following error:
Unknown "form_help" function. Did you mean "form_rest", "form_end"?
the form_div_layout.html.twig contains the code found at github symfony twig form theme
At my config.yml I've added the following under the twig section,
form_themes:
- 'form/form_div_layout.html.twig'
.
either not, i still have this error
what is missing ???
My file structure
If all your forms are going to use the same theme you only need to add the line in your config, but if you want a particular form theme in a particular template you can use the template tag.
The reason you're getting the 'form is not defined error' is because you don't have a variable called form passed the the template, your form variable is called addpost, so you need to use
{% form_theme addpost 'form/form_div_layout.html.twig' %}

Twig Runtime Error: Impossible to invoke a method ("test") on a string variable

I have the following twig template (the code is in the same file):
{% macro renderJob(fields) %}
// renders the job UI block, but I've removed it for simplicity
Hello world.
{% endmacro %}
{% block _jobs_widget %}
<div id="jobsContainer">
{% for fields in form.children %}
{% dump fields %}
{{ _self.renderJob(fields) }}
{% endfor %}
</div>
{% endblock %}
For some reason, after upgrading to twig/twig = v2.1.0 I'm receiving the follwing error:
Impossible to invoke a method ("renderJob") on a string variable ("#AppBundle/Jobs/form/job.html.twig").
I have been trying to figure out what's causing this without any luck. This used to work just fine in 1.3.x. The fields variable contains the proper data, but it appears it can't pass it to the renderJob macro or it can't find the macro (which is kind of odd)?
Have you tried the following ?
{% import _self as renderJobMacro %}
{% macro renderJob(fields) %}
// renders the job UI block, but I've removed it for simplicity
Hello world.
{% endmacro %}
{% block _jobs_widget %}
<div id="jobsContainer">
{% for fields in form.children %}
{{ renderJobMacro.renderJob(fields) }}
{% endfor %}
</div>
{% endblock %}
I think _self is depricated from twigg 2.0, May be you need to check without _self.
Check {{ renderJob(fields) }} instead of {{ _self.renderJob(fields) }}

Symfony Twig exception

I am trying to use a HTML table bundle:
https://github.com/ekyna/TableBundle
Here is the calling code:
$table = $this->get('table.factory')->createBuilder(
new InventoryType(),
['name' => 'project_inventory_list']
)->getTable($request);
$content = ['content_area' => $table->createView()];
return $this->render('MyProjectBundle:Default:index.html.twig', $content);
I am getting an exception:
An exception has been thrown during the rendering of a template
("Catchable Fatal Error: Object of class
Ekyna\Component\Table\TableView could not be converted to string") in
MyProjectBundle:Default:index.html.twig at line 6.
Stepped through code not sure whats going on - hoping it's a trivial issue???
| EDIT
{% extends 'mYThemeBundle:layout:base-layout.html.twig' %}
{% block title %}HEADER{% endblock %}
{% block page_content %}
{{ content_area }}
{% endblock %}
In your TWIG template, you have to do the following:
{% extends 'mYThemeBundle:layout:base-layout.html.twig' %}
{% block title %}HEADER{% endblock %}
{% block page_content %}
{{ ekyna_table_render(content_area ) }}
{% endblock %}
You need to use the TWIG function ekyna_table_render to render the table view.
After installing EkynaBundle (documentation was not up to date) I also needed to adjust the template as demonstrated above in addition to:
Install and enable WhiteOctoberPagerfantaBundle
Install and enable BraincraftedBootstrapBundle
Install twig extensions
Install and enable LiipImagineBundle
Thanks for all the help.

Problems with include

i have a lot of time programming in PHP, but im doing my first steps in Symfony.
Im try write Twigg templates, i have a public template in app/Resources/view/public.html.twig.
This file contains: http://pastebin.com/T1KGMfXL.
Now, in CloudBundle, have a base.html.twig:
{% extends '::public.html.twig' %}
{% block main %}
<div class="login_page">
<div class="login_box">
{% block content %} {% endblock %}
</div>
</div>
{% endblock %}
And the content in another twig file. For example, login.html.twig
{% extends 'base.html.twig' %}
{% block content %}
....
{% endblock %}
In the Controller, when an user try http://cloud.man.local/app.php/login:
public function staticAction($sitio)
{
// in this case, $sitio contains "login"
return $this->render("CloudBundle:Default:$sitio.html.twig");
}
So, the problem is that, only shows the footer, not show the content.
Any ideas ?.
You want to override a block which is inside another block, so in your case I think you should try this in the another twig files:
{% block main %}
{{ parent() }}
{% block content %} YOur content {% endblock %}
{% endblock %}
see http://twig.sensiolabs.org/doc/functions/parent.html
Hope it's helpful.
Best regard.

Resources