在模板中的引用字符串

huangapple go评论103阅读模式
英文:

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 (
	&quot;log&quot;
	&quot;os&quot;
	&quot;html/template&quot;
)

type TemplateData struct {
	Email *string
}

func main() {

	const temp = &quot;&lt;script&gt;var email = {{.Email}};&lt;/script&gt;\n&quot;

	t := template.Must(template.New(&quot;email_template&quot;).Parse(temp))
	email := &quot;somebody@somewhere.com&quot;
	err := t.Execute(os.Stdout, TemplateData{
		Email: &amp;email,
	})
	if err != nil {
		log.Println(&quot;executing template:&quot;, err)
	}
	err = t.Execute(os.Stdout, TemplateData{
		Email: nil,
	})
	if err != nil {
		log.Println(&quot;executing template:&quot;, err)
	}

}

Trying to use a "null" string:

package main

import (
	&quot;log&quot;
	&quot;os&quot;
	&quot;html/template&quot;
)

type TemplateData struct {
	Email string
}

func main() {

	const temp = &quot;&lt;script&gt;var email = {{.Email}};&lt;/script&gt;\n&quot;

	t := template.Must(template.New(&quot;email_template&quot;).Parse(temp))
	email := &quot;somebody@somewhere.com&quot;
	err := t.Execute(os.Stdout, TemplateData{
		Email: email,
	})
	if err != nil {
		log.Println(&quot;executing template:&quot;, err)
	}
	err = t.Execute(os.Stdout, TemplateData{
	})
	if err != nil {
		log.Println(&quot;executing template:&quot;, 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, &amp;TplCtx{nil})

huangapple
  • 本文由 发表于 2013年1月4日 12:14:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/14150985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定