Symfony2: Custom Error Page Extend base.html.twig

安稳与你 提交于 2019-12-01 15:38:22

问题


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?


回答1:


file should be located in app/Resources/views/Exception/

instead of

app/Resources/TwigBundle/views/Exception/




回答2:


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



回答3:


// app/Resouces/views/base.html.twig

×
{% include('path/to/include') %}

○
{% include('::path/to/include') %}



回答4:


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.




回答5:


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


来源:https://stackoverflow.com/questions/22111807/symfony2-custom-error-page-extend-base-html-twig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!