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.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料