gpt4 book ai didi

go - 在golang中执行不带引号的模板

转载 作者:IT王子 更新时间:2023-10-29 01:43:09 26 4
gpt4 key购买 nike

我想用 noescape 执行模板并且没有引号,但是现在不支持 noescape

有什么建议或者我必须使用另一个模板引擎吗?谢谢!

代码在这里:http://play.golang.org/p/R-Ib5H9bXx

最佳答案

我们鼓励您将安全的 Javascript 存储在 template.JS 类型中:

type JS string

JS encapsulates a known safe EcmaScript5 Expression, for example, (x + y * z()). Template authors are responsible for ensuring that typed expressions do not break the intended precedence and that there is no statement/expression ambiguity as when passing an expression like "{ foo: bar() }\n'foo'", which is both a valid Expression and a valid Program with a very different meaning.

因此,您需要对代码做的唯一更改是:

type Var struct {
Name template.JS
Value template.JS
}

关于go - 在golang中执行不带引号的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25130946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com