douyiqi9640 2015-03-26 11:38
浏览 49

Twig中的{{exception.message}}不呈现HTML

I have this controller where \Exception is raised (I haven't figured out which SF2 Exception to use yet) upon certain condition. Here is it:

<?php

namespace My\AppBundle\Controller;

use ....

class MyController extends Controller
{
     const EXCEPTION_MESSAGE = <<<EOF
My <b>HTML</b>
<br/>
<small>Small phrase</small>
EOF;

     public function indexAction()
     {
         // my logic

         if(in_array($data, $array))
             throw new \Exception(self::EXCEPTION_MESSAGE);

         // the rest of my logic

         return ....
     }

}

And in app/Resources/TwigBundle/views/Exception/error.html.twig

{% extends '::base.html.twig' %}

{% block body %}
    <h2>Error</h2>
    <p>{{ exception.message }}</p>
{% endblock %}

The problem is HTML is not rendered when seeing the error page in prod environement.

I tried {{ exception.message|raw }} and also setting autoescape to false as per this answer but it seems to have no effect.

How can I do to make HTML works when displaying the \Exception message in Twig?

  • 写回答

1条回答 默认 最新

  • duannv2081 2015-03-26 12:46
    关注

    Where ever in the code you catch the exception is where it is needed to be added to the array you pass to twig. for example

    $vars = [];
    try {
    
        $a->indexAction();
        //fill $vars 
    
    
    } catch (Exception $e) {
      $vars['exception'] = $e;
    }
    
    //pass $vars to twig
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题