How to use go template to parse html files with FuncMap

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

How to use go template to parse html files with FuncMap

问题

我使用以下代码解析HTML模板。它工作得很好。

func test(w http.ResponseWriter, req *http.Request) {
    data := struct {
        A int
        B int
    }{
        A: 2,
        B: 3,
    }

    t := template.New("test.html").Funcs(template.FuncMap{"add": add})

    t, err := t.ParseFiles("test.html")

    if err != nil {
        log.Println(err)
    }

    t.Execute(w, data)
}

func add(a, b int) int {
    return a + b
}

还有HTML模板test.html。

<html>
<head>
    <title></title>
</head>
<body>
    <input type="text" value="{{add .A .B}}">
</body>
</html>

但是当我将HTML文件移动到另一个目录时,然后使用以下代码时,输出始终为空。

t := template.New("./templates/test.html").Funcs(template.FuncMap{"add": add})

t, err := t.ParseFiles("./templates/test.html")

有人可以告诉我出了什么问题吗?或者html/template包不能像这样使用吗?

英文:

I use following code to parse html template. It works well.

func test(w http.ResponseWriter, req *http.Request) {

    data := struct {A int B int	}{A: 2,	B: 3}

    t := template.New(&quot;test.html&quot;).Funcs(template.FuncMap{&quot;add&quot;: add})

    t, err := t.ParseFiles(&quot;test.html&quot;)

    if err!=nil{
        log.Println(err)
    }
    t.Execute(w, data)
}

func add(a, b int) int {
    return a + b
}

and html template test.html.

&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;input type=&quot;text&quot; value=&quot;{{add .A .B}}&quot;&gt;
&lt;/body&gt;
&lt;/html&gt;

But when I move html file to another directory. Then use the following code. The output is always empty.

t := template.New(&quot;./templates/test.html&quot;).Funcs(template.FuncMap{&quot;add&quot;: add})

t, err := t.ParseFiles(&quot;./templates/test.html&quot;)

Can anyone tell me what's wrong? Or html/template package can not be used like this?

答案1

得分: 2

你的程序(html/template包)无法找到test.html文件,这是出了问题的原因。当你指定相对路径(你的路径是相对路径)时,它们会解析为当前工作目录。

你必须确保HTML文件/模板放在正确的位置。例如,如果你使用go run ...启动应用程序,相对路径将解析为你所在的文件夹,也就是当前工作目录。

这个相对路径:"./templates/test.html"将尝试解析位于当前文件夹的templates子文件夹中的文件。确保它在那里。

另一种选择是使用绝对路径。

**还有另一个重要的注意事项:**不要在处理程序函数中解析模板!这会在为每个传入的请求提供服务时运行。相反,应该在包的init()函数中解析它们一次。

关于这个问题的更多细节,请参考以下链接:

https://stackoverflow.com/questions/28451675/it-takes-too-much-time-when-using-template-package-to-generate-a-dynamic-web-p

英文:

What's wrong is that your program (the html/template package) can't find the test.html file. When you specify relative paths (yours are relative), they are resolved to the current working directory.

You have to make sure the html files/templates are in the right place. If you start your app with go run ... for example, relative paths are resolved to the folder you're in, that will be the working directory.

This relative path: &quot;./templates/test.html&quot; will try to parse the file being in the templates subfolder of the current folder. Make sure it's there.

Another option is to use absolute paths.

And also another important note: Do not parse templates in your handler function! That runs to serve each incoming request. Instead parse them in the package init() functions once.

More details on this:

https://stackoverflow.com/questions/28451675/it-takes-too-much-time-when-using-template-package-to-generate-a-dynamic-web-p

huangapple
  • 本文由 发表于 2015年12月3日 13:50:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/34058772.html
匿名

发表评论

匿名网友

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

确定