英文:
Disable method evaluation inside template
问题
我正在使用Go进行模板评估,以下是我的用例。
我们的应用程序要求客户在文本中包含基于前缀的占位符。
例如:
我的名字是{$.PRE.FIRSTNAME}
这将自动替换为他们的名字,这只是一个简单的文本,客户还可以输入更复杂的文本,甚至可以是HTML。
以下是代码片段:
在这里,第一个templateText正常工作,而第二个被注释的部分不起作用。它会抛出一个未定义函数的异常。
package main
import (
"bytes"
"html/template"
"log"
)
// OuterObject object
type OuterObject struct {
PRE *InnerObject
}
// InnerObject macro object
type InnerObject struct {
FIRSTNAME, LASTNAME string
}
func main() {
// This works perfectly.
templateText := "我的名字是{$.PRE.FIRSTNAME}"
// templateText := "我的名字是{$.PRE.FIRSTNAME} ().push(function() { globalAml.display('sss') };"
//
// 上面的templateText会抛出异常:
// 函数"globalAml"未定义
//
o := &OuterObject{
PRE: &InnerObject{
FIRSTNAME: "fName",
LASTNAME: "lName",
},
}
var doc bytes.Buffer
t, err := template.New("test").Delims("{", "}").Parse(templateText)
if err != nil {
log.Fatal("解析标签错误", err)
}
err1 := t.Execute(&doc, o)
if err1 != nil {
log.Println("执行标签错误", err1)
}
log.Println("最终文本", doc.String())
}
有人可以帮助我忽略方法调用,只替换基于前缀的占位符吗?
英文:
I am using Go for template evaluation and the following is my use case.
We have our application in which we ask customers to include prefixed-based placeholders along with their text.
E.g.:
my name is {$.PRE.FIRSTNAME}
This will be automatically replaced with their first name, this is just a simple text, there will be more complex text which will be entered by the customer. it can even be a HTML.
following is the code snippet
Over here the first templateText works perfectly file and the second commented one doesn't work. it throws an exception stating a function not defined.
package main
import (
"bytes"
"html/template"
"log"
)
// OuterObject object
type OuterObject struct {
PRE *InnerObject
}
// InnerObject macro object
type InnerObject struct {
FIRSTNAME, LASTNAME string
}
func main() {
// This works perfectly.
templateText := "my name is {$.PRE.FIRSTNAME} "
// templateText := "my name is {$.PRE.FIRSTNAME} ().push(function() { globalAml.display('sss') };"
//
// Above templateText fails with exception :
// function "globalAml" not defined
//
o := &OuterObject{
PRE: &InnerObject{
FIRSTNAME: "fName",
LASTNAME: "lName",
},
}
var doc bytes.Buffer
t, err := template.New("test").Delims("{", "}").Parse(templateText)
if err != nil {
log.Fatal("Error parsing tag", err)
}
err1 := t.Execute(&doc, o)
if err1 != nil {
log.Println("Error Execute tag", err1)
}
log.Println("Final text ", doc.String())
}
Can anyone help me to ignore the method calls and just to replace the prefixed-based placeholders.
答案1
得分: 2
我暂时通过将代码片段更改为以下内容来处理这个案例:
t, err := template.New("test").Delims("{$", "}").Parse(templateText)
我知道在客户输入包含上述分隔符的文本时,这种方法会失败。
非常感谢,伙计们。
英文:
I was temporarily able to handle this case by changing the code snippet to following :
t, err := template.New("test").Delims("{$", "}").Parse(templateText)
I know this will fail in cases where customer entering a text containing these above delimiters.
Thanks a lot, guys.
答案2
得分: 0
你的第二次替换失败是因为 {
和 }
是模板的分隔符。你需要对它们进行引用才能使其正常工作:
templateText := "my name is {$.PRE.FIRSTNAME} ().push(function() {`{`} globalAml.display('sss') {`}`};""
英文:
Your second substitution fails because {
and }
are the template delimiters. You need to quote them to make this work:
templateText := "my name is {$.PRE.FIRSTNAME} ().push(function() {`{`} globalAml.display('sss') {`}`};"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论