如何在 IRIS 中禁用自动转义
How to disable AutoEscaping in IRIS
我将 HTML 标签插入数据库 table:
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
并将检索到的数据发送到视图中
func MyEvent(ctx iris.Context){
rows := ...
ctx.ViewData("rows", rows[0])
ctx.View("template.html")
}
如何才能在此事件中禁用自动转义?
如何通过模板获取原始 HTML 并进入输出将取决于您与 Iris 一起使用的模板引擎。 Iris supports five template engines out-of-the-box:
- The standard html, its template parser is the golang.org/pkg/html/template/
- Django, its template parser is the github.com/flosch/pongo2
- Pug(Jade), its template parser is the github.com/Joker/jade
- Handlebars, its template parser is the github.com/aymerick/raymond
- Amber, its template parser is the github.com/eknkc/amber
如果您使用的是标准 html/template
包,那么您可以使用 template.HTML
类型将字符串标记为 "safe HTML":
ctx.ViewData("rows", template.HTML(rows[0]))
或添加您自己的过滤器,它只执行 return template.HTML(s)
并在模板中使用它。
如果您使用的是 Handlebars,那么您将使用 {{{...}}}
in the template or raymond.SafeString
in a helper:
{{{yourHTML}}}
如果您使用的是其他模板引擎之一,那么您可以使用它们提供的任何机制来通过模板获取原始 HTML。
当然,所有这些都假定您在 HTML 进入数据库之前或从数据库到模板之前对其进行清理和清理。
我将 HTML 标签插入数据库 table:
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
并将检索到的数据发送到视图中
func MyEvent(ctx iris.Context){
rows := ...
ctx.ViewData("rows", rows[0])
ctx.View("template.html")
}
如何才能在此事件中禁用自动转义?
如何通过模板获取原始 HTML 并进入输出将取决于您与 Iris 一起使用的模板引擎。 Iris supports five template engines out-of-the-box:
- The standard html, its template parser is the golang.org/pkg/html/template/
- Django, its template parser is the github.com/flosch/pongo2
- Pug(Jade), its template parser is the github.com/Joker/jade
- Handlebars, its template parser is the github.com/aymerick/raymond
- Amber, its template parser is the github.com/eknkc/amber
如果您使用的是标准 html/template
包,那么您可以使用 template.HTML
类型将字符串标记为 "safe HTML":
ctx.ViewData("rows", template.HTML(rows[0]))
或添加您自己的过滤器,它只执行 return template.HTML(s)
并在模板中使用它。
如果您使用的是 Handlebars,那么您将使用 {{{...}}}
in the template or raymond.SafeString
in a helper:
{{{yourHTML}}}
如果您使用的是其他模板引擎之一,那么您可以使用它们提供的任何机制来通过模板获取原始 HTML。
当然,所有这些都假定您在 HTML 进入数据库之前或从数据库到模板之前对其进行清理和清理。