小编Ran*_*tor的帖子

在模板中,如何在"with"或"range"范围内访问外部范围?

在里面with或者range,范围.改变了.你如何访问调用范围?

go go-templates

36
推荐指数
2
解决办法
8522
查看次数

为什么我在Go HTML模板输出中看到ZgotmplZ?

当我调用Go模板函数输出HTML时,它会显示ZgotmplZ.

示例代码:

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

package main

import (
    "html/template"
    "os"
)

func main() {
    funcMap := template.FuncMap{
        "printSelected": func(s string) string {
            if s == "test" {
                return `selected="selected"`
            }
            return ""
        },

        "safe": func(s string) template.HTML {
            return template.HTML(s)
        },
    }
    template.Must(template.New("Template").Funcs(funcMap).Parse(`
    <option {{ printSelected "test" }} {{ printSelected "test" | safe }} >test</option>
    `)).Execute(os.Stdout, nil)

}
Run Code Online (Sandbox Code Playgroud)

输出:

<option ZgotmplZ ZgotmplZ >test</option>
Run Code Online (Sandbox Code Playgroud)

go go-templates

26
推荐指数
4
解决办法
5392
查看次数

标签 统计

go ×2

go-templates ×2