How to display string that contains HTML in twig template?

前端 未结 4 614
刺人心
刺人心 2020-11-30 20:19

How can I display a string that contains HTML tags in twig template?

My PHP variable contains this html and text:

$word = \' a word 

        
相关标签:
4条回答
  • 2020-11-30 20:33

    Use raw keyword, http://twig.sensiolabs.org/doc/api.html#escaper-extension

    {{ word | raw }}
    
    0 讨论(0)
  • 2020-11-30 20:37

    if you don't need variable, you can define text in
    translations/messages.en.yaml :
    CiteExampleHtmlCode: "<b> my static text </b>"

    then use it with twig:
    templates/about/index.html.twig
    … {{ 'CiteExampleHtmlCode' }}
    or if you need multilangages like me:
    … {{ 'CiteExampleHtmlCode' | trans }}

    Let's have a look of https://symfony.com/doc/current/translation.html for more information about translations use.

    0 讨论(0)
  • 2020-11-30 20:43
    {{ word|striptags('<b>,<a>,<pre>')|raw }}
    

    if you want to allow multiple tags

    0 讨论(0)
  • 2020-11-30 20:48

    You can also use:

    {{ word|striptags('<b>')|raw }}
    

    so that only <b> tag will be allowed.

    0 讨论(0)
提交回复
热议问题