如何从template.FuncMap返回HTML模板?

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

How to return a HTML template from a template.FuncMap?

问题

我在https://groups.google.com/forum/#!topic/golang-nuts/CUbdaHkESJk上提出了这个问题,但没有收到回复。

我正在编写一个基于Web的媒体查看器,但我在如何标记不同的媒体方面遇到了问题。

我目前的代码在这里:https://github.com/kaihendry/lk/blob/5de96f9fe012e9894deef7b9924f96dd8d9c806c/main.go#L181
肯定是错误的。我对如何在这里使用模板感到困惑,参考了https://golang.org/pkg/html/template/#Template.Execute

我看到的所有使用template.FuncMap的示例都只使用字符串...

简化的示例:https://play.golang.org/p/mAue8SDDw6

理想情况下,我想在这里使用HTML模板而不是fmt.Sprintf。是的,我意识到我没有对文件名进行HTML转义,这是错误的,但我不确定如何再次从这个函数中使用HTML模板。

提前感谢您的任何指导。

英文:

I asked this question upon https://groups.google.com/forum/#!topic/golang-nuts/CUbdaHkESJk but received no replies.

I'm writing a Web based media viewer and I've hit a snag about how to mark up different media.

My current code here: https://github.com/kaihendry/lk/blob/5de96f9fe012e9894deef7b9924f96dd8d9c806c/main.go#L181
is certainly wrong. I am puzzled how to use a template here in light of https://golang.org/pkg/html/template/#Template.Execute

All the examples I see of using template.FuncMap just use strings...

Reduced example: https://play.golang.org/p/mAue8SDDw6

Ideally I would use html templates here and not fmt.Sprintf. Yes, I realise I am not HTML escaping the filename which is wrong, but I am not sure how to use HTML templates again from this function.

Thanks in advance for any guidance,

答案1

得分: 3

你应该将逻辑(函数)与展示(模板)分开。
template.FuncMap中注册的函数不应该依赖于模板作为其输入来产生输出。如果你想将HTML模板作为函数的输出返回,你应该手动生成它(使用fmt.Sprintf等方法)。

在你的情况下,你可以简单地注册一个函数来检查媒体类型,然后使用模板的{{if}}动作生成不同的输出。这个函数可能如下所示:

func matchType(ext, s string) bool {
    return strings.ToLower(ext) == strings.ToLower(path.Ext(s))
}

模板可能如下所示:

{{ range .Media }}<p>
    {{if . | matchType ".jpg"}}<img src={{.}}>
    {{else if . | matchType ".mp4"}}<video controls src={{.}}></video>
    {{else}}{{.}}
    {{end}}</p>
{{ end }}

基于你提供的简化示例的工作示例:https://play.golang.org/p/U64_7UHZQU

英文:

You should separate the logic (function) from presentation (template).
The function registered in template.FuncMap should not depends on template as its input to produce an output. If you want to return HTML template as the function output, you should generate it manually (using fmt.Sprintf, etc.).

In your case, you can simply register a function to check the media type, then generate different output using template's {{if}} action. The function may looks like:

func matchType(ext, s string) bool {
    return strings.ToLower(ext) == strings.ToLower(path.Ext(s))
}

and the template looks like:

{{ range .Media }}&lt;p&gt;
    {{if . | matchType &quot;.jpg&quot;}}&lt;img src={{.}}&gt;
    {{else if . | matchType &quot;.mp4&quot;}}&lt;video controls src={{.}}&gt;&lt;/video&gt;
    {{else}}{{.}}
    {{end}}&lt;/p&gt;
{{ end }}

A working example based on your reduced example: https://play.golang.org/p/U64_7UHZQU

huangapple
  • 本文由 发表于 2017年8月15日 10:09:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/45685547.html
匿名

发表评论

匿名网友

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

确定