doumo2501 2017-03-24 10:10
浏览 108

php twig模板html代码

I have a problem relation with twig template.

final class index {

public $request;
private $loader;
private $twig;

/**
 * Constructor.
 * definition : class preloader with default
 * symfony component request class
 * twig template configuration
 * @param type dependency injection and function
 */
public function __construct(){

    //get request info
    $this->request=new request();
    $this->loader = new \Twig_Loader_Filesystem(root.'/src/declarations/twigTemplate');
    $this->twig = new \Twig_Environment($this->loader, array(
        //'cache' => root.'/src/declarations/twigTemplate/cache',

    ));
}

/**
 * get declaration main function.
 * definition:index method is defined in a declaration
 * and it is called as https://ip/company/service/app/service/doc
 * @param type dependency injection and function
 * @return array
 */
public function index(){

    //return
    return $this->twig->render("index.twig",['var'=>'foo']);

}

}

my twig file outputs as same html codes.namely,it doesn't run html codes.

index.twig file :

<strong>{{ var }}</strong>

output :

<strong>foo</strong>

I can solve this problem.already thanks for your answers

  • 写回答

1条回答 默认 最新

  • dorbmd1177 2017-03-24 10:41
    关注

    You have to use autoescape tags and/or Raw tags

    {% autoescape %}
        {{ var|raw }} {# var won't be escaped #}
    {% endautoescape %} 
    

    So lets say this:

    {% set example= "<h1>Test</h1>" %}
    {{ example|raw }} <-- This will output Test in H1 
    {{ example }} <-- This will output <h1>Test</h1> 
    

    I know you had already found an answer to your problem. but if others have this problem this are solutions to outputting HTML in twig. Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大