如何在html代码中打印树枝变量 [英] How to print twig variables inside html code

查看:36
本文介绍了如何在html代码中打印树枝变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个变量,如下所示:

I created a variable, like this :

{% set checkboxHTML = '<button class="btn btn-default btn-md" data-id="{{b.id}}">Edit</button>' %}

现在我尝试以两种方式打印:

Now I trying print in two ways:

  1. {{ checkboxHTML }}

上面像文本一样打印(没有 html)

Above it printed like text (no html)

  1. {{ checkboxHTML |原始}}

打印 html,但变量 {{ b.id }} 不像 twig sintax 那样接受它,像文本一样接受它

Print the html, but the variable {{ b.id }} doesn't take it like twig sintax, take it like text

如何在文本中打印该变量?

How to print that variable inside a text ?

推荐答案

解决这个问题的替代方法是使用扩展的 {% set %}
使用此方法时请注意,内容被认为是安全的

Alternative method to solve this is using the expanded {% set %}
Do note when using this method, the content is considered being safe

{% set checkboxHTML %}
    <button class="btn btn-default btn-md" data-id="{{ b.id }}">Edit</button>
{% endset %}

twigfiddle

这篇关于如何在html代码中打印树枝变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆