Go HTML模板:我可以阻止模板包在脚本中的字符串周围插入引号吗?

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

Go HTML templates: Can I stop the templates package inserting quotes around strings in scripts?

问题

我明白你的问题。你想在模板的正文中输出与图像关联的根URL,但是当你这样做时,模板包会尝试在其周围加上引号。你想知道是否有解决这个问题的方法。

根据你提供的代码,我看到你使用了template.JSStr函数来将根URL转换为JavaScript字符串。这可能导致在输出时添加引号。为了解决这个问题,你可以尝试使用template.URL类型来表示根URL,而不是使用template.JSStr

修改Data结构体的定义,将IMG_ROOT的类型更改为template.URL,如下所示:

type Data struct {
    IMG_ROOT template.URL
}

然后,在main函数中,将template.JSStr替换为template.URL,如下所示:

t.Execute(os.Stdout, &Data{template.URL("/path/to/file")})

这样修改后,模板输出中的根URL将不再被包裹在引号中。

希望这可以解决你的问题!如果还有其他疑问,请随时提问。

英文:

All of my templates have a variable indicating the root url of their associated images. I want to output that root before the filename of the image in the body of the template, but when I do so, the templates package attempts to put quotes around it. Here's a minimal piece of code that shows my problem. IMG_ROOT is an interface in this example to better simulate the real code. The script type is text/template because its contents will be used in an underscore.js template. The type doesn't appear to matter to how it's output though.

package main

import (
	"html/template"
	"os"
)

type Data struct {
	IMG_ROOT interface{}
}

const tmpl = `
<body>
<script type="text/template">
	<img src="{{.IMG_ROOT}}/file_name.jpg"></img>
</script>
</body>

`

func main() {
	t, _ := template.New("").Parse(tmpl)
	t.Execute( os.Stdout, &Data{ template.JSStr( "/path/to/file" ) } )
}

http://play.golang.org/p/wg9JK2w2lt

If I write <img src="{{.IMG_ROOT}}/file_name.jpg"></img>, I get "\/path\/to\/file/file_name.jpg" which isn't valid.

If I write <img src={{.IMG_ROOT}}/file_name.jpg></img>, I get "/path/to/file/"file_name.jpg which doesn't work either.

Is there a workaround for this? I've tried using every 'safe' type from the templates package for the IMG_ROOT type, but haven't had any luck. I could just write a function that concatenates and returns the root and file, but it seems like there should be a simpler solution.

Any ideas?

Thanks!

答案1

得分: 5

你可以使用内置的printf函数:

<img src={{printf "%s/file_name.jpg" .IMG_ROOT}}></img>

但是,我建议创建一个自定义的辅助函数,并使用它来创建这样的路径:

const ImageRoot = "path/to/files/"

func StaticFile(filename string) string {
    return ImageRoot + filename
}

func main() {
    t, _ := template.New("").Funcs(template.FuncMap{"staticFile": StaticFile}).Parse(tmpl)
    t.Execute(os.Stdout, nil)
}

然后在你的模板中,你可以这样做:

<img src="{{staticFile "image.jpg"}}"></img>
英文:

You could use printf built-in function:

&lt;img src={{printf &quot;%s/file_name.jpg&quot; .IMG_ROOT}}&gt;&lt;/img&gt;

http://play.golang.org/p/2v8Q6wHu0r

But instead, I would recommend creating a custom helper function and use it to create such paths:

const ImageRoot = &quot;path/to/files/&quot;

func StaticFile(filename string) string {
    return ImageRoot + filename
}

func main() {
    t, _ := template.New(&quot;&quot;).Funcs(template.FuncMap{&quot;staticFile&quot;: StaticFile}).Parse(tmpl)
    t.Execute(os.Stdout, nil)
}

And then in your templates you can just do

&lt;img src=&quot;{{staticFile &quot;image.jpg&quot;}}&quot;&gt;&lt;/img&gt;

http://play.golang.org/p/NQTHmqNeT0

答案2

得分: 1

这是因为html/template会对内容进行转义,以防止攻击等问题。我遇到了类似的问题,后来切换到text/template后,模板的行为符合预期。不过,我会使用其他方法来获取所需的输出,并继续使用html/template

英文:

It's because html/template escapes things to prevent attacks, etc. I came to this question with a similar problem. Switching to text/template made my template behave as expected. That said, I'll just use another method to get the output I need and continue to use html/template.

huangapple
  • 本文由 发表于 2014年6月25日 22:47:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/24411880.html
匿名

发表评论

匿名网友

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

确定