英文:
Quoted string in templates
问题
var email = null;
// or a string:
var email = "somebody@somewhere.com";
但是使用模板
var email = {{.Email}};
我每次都得到带引号的字符串:
var email = "null";
var email = "somebody@somewhere.com";
如何修复它?
编辑:这是我的代码:http://play.golang.org/p/8k4s8dv2PE
你可以看到Go用引号包围字符串并删除注释 - 这是预处理或后处理。
英文:
I want to put in my html template (actually JavaScript part) null or string with email:
var email = null;
// or a string:
var email = "somebody@somewhere.com";
But with template
var email = {{.Email}};
I get quoted string everytime:
var email = "null";
var email = "somebody@somewhere.com";
How to fix it?
EDIT: here is my code: http://play.golang.org/p/8k4s8dv2PE
You can see Go surrounds string with quotes and removes comment - there is pre or post processing.
答案1
得分: 5
没有看到你的代码,我能做的最好的就是提供一个展示你所需功能的示例。如果我猜的话,我会说你传递的数据是一个字符串,而不是一个字符串指针。一个字符串不能有一个值为nil/null,只有一个字符串指针可以。虽然这会产生一个"",而不是一个"null",所以我只能想象这是你在代码中做一些奇怪的事情。为了防止链接失效,我在下面复制了代码示例,不过点击链接查看它们的输出效果会更好。
所需功能:
package main
import (
"log"
"os"
"html/template"
)
type TemplateData struct {
Email *string
}
func main() {
const temp = "<script>var email = {{.Email}};</script>\n"
t := template.Must(template.New("email_template").Parse(temp))
email := "somebody@somewhere.com"
err := t.Execute(os.Stdout, TemplateData{
Email: &email,
})
if err != nil {
log.Println("executing template:", err)
}
err = t.Execute(os.Stdout, TemplateData{
Email: nil,
})
if err != nil {
log.Println("executing template:", err)
}
}
尝试使用"null"字符串:
package main
import (
"log"
"os"
"html/template"
)
type TemplateData struct {
Email string
}
func main() {
const temp = "<script>var email = {{.Email}};</script>\n"
t := template.Must(template.New("email_template").Parse(temp))
email := "somebody@somewhere.com"
err := t.Execute(os.Stdout, TemplateData{
Email: email,
})
if err != nil {
log.Println("executing template:", err)
}
err = t.Execute(os.Stdout, TemplateData{
})
if err != nil {
log.Println("executing template:", err)
}
}
英文:
Without seeing your code, the best I can do is provide an example that shows your required functionality.
If I had to take a guess, I'd say the data you were passing in was a string, not a string pointer. A string cannot have a value of nil/null, only a string pointer can. Though that would yield a "", not a "null", (as demonstrated here), so I can only imagine that this is something weird you're doing in your code.
To prevent link-rot, I've duplicated the code examples below, though it's worth clicking through the links to see their output.
Required Functionality:
package main
import (
"log"
"os"
"html/template"
)
type TemplateData struct {
Email *string
}
func main() {
const temp = "<script>var email = {{.Email}};</script>\n"
t := template.Must(template.New("email_template").Parse(temp))
email := "somebody@somewhere.com"
err := t.Execute(os.Stdout, TemplateData{
Email: &email,
})
if err != nil {
log.Println("executing template:", err)
}
err = t.Execute(os.Stdout, TemplateData{
Email: nil,
})
if err != nil {
log.Println("executing template:", err)
}
}
Trying to use a "null" string:
package main
import (
"log"
"os"
"html/template"
)
type TemplateData struct {
Email string
}
func main() {
const temp = "<script>var email = {{.Email}};</script>\n"
t := template.Must(template.New("email_template").Parse(temp))
email := "somebody@somewhere.com"
err := t.Execute(os.Stdout, TemplateData{
Email: email,
})
if err != nil {
log.Println("executing template:", err)
}
err = t.Execute(os.Stdout, TemplateData{
})
if err != nil {
log.Println("executing template:", err)
}
}
答案2
得分: 0
使用最少的编辑来修改您的代码示例:
type TplCtx struct {
UserEmail *string
}
和
t.Execute(os.Stdout, &TplCtx{nil})
英文:
Using the least amount of editing to your code sample:
type TplCtx struct {
UserEmail *string
}
and
t.Execute(os.Stdout, &TplCtx{nil})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论