dongshi7350 2013-10-30 09:55
浏览 124
已采纳

Twig:如何在块标题中显示html内容

In my symfony2 project, I have some twig temlates. In one of them, I have the block title.

{% block title %}{{ announcement.title }}{% endblock %}

The problem is that the variable {{ announcement.title }} can be: <em>test</em>

In this case, my title will be <em>test</em> and it needs to be

test

I have tryied the {% block title %}{{ announcement.title|raw }}{% endblock %} but nothing changed. This is only in the block title, if I put the raw filter in my block content, then it is fine.

Any idea on how to do that ?

EDIT:

Here is an example:

{% block title %}{{ announcement.title|raw }}{% endblock %}
{% block body_announcements %}{{ announcement.title|raw }}{% endblock %}

In the block title, I see <em>test<em> and in the block body_announcements I see test which is right.

  • 写回答

3条回答 默认 最新

  • duanlu1908 2013-10-30 09:58
    关注

    You have to use the raw filter to display text and tags as is:

    {% block title %}{{ announcement.title|raw }}{% endblock %}
    

    Edit: The striptags filter is used to display text without the tags:

    {% block title %}{{ announcement.title|striptags }}{% endblock %}
    

    Example

    Twig code

    {% set str = '<em>test</em>' %}
    1: {{ str }}
    2: {{ str|raw }}
    3: {{ str|striptags }}
    

    Result

    1: <em>test</em>

    The <em></em> tag is escaped.

    2: test

    The <em></em> tag is displayed and interpreted by the browser.

    3: test

    The <em></em> tag is removed, only the text is shown.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂