如何使用go模板来解析带有FuncMap的html文件 [英] How to use go template to parse html files with FuncMap

查看:533
本文介绍了如何使用go模板来解析带有FuncMap的html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码来解析html模板。它运作良好。

  func测试(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))
$ bt,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 =textvalue ={{add .A .B}}>
< / body>
< / html>

但是当我将html文件移动到另一个目录时。然后使用下面的代码。

  t:= template.New(./ templates / test.html)。Funcs( template.FuncMap {add:add})
$ b $,err:= t.ParseFiles(./ templates / test.html)

任何人都可以告诉我什么是错的吗?或者html /模板包不能像这样使用?

解决方案

有什么问题是您的程序( html / template package )找不到 test.html 文件。当你指定相对路径(你的是相对路径)时,它们被解析到当前工作目录。



你必须确保html文件/模板在正确的位置。例如,如果您使用 go run ... 来启动应用程序,则相对路径将解析为您所在的文件夹,即工作目录。



这个相对路径:./ templates / test.html会尝试解析<$ c $中的文件c>模板当前文件夹的子文件夹。确保它在那里。



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



注意:不要在您的处理函数中分析模板!它运行为每个传入的请求提供服务。相反,请将它们解析到包 init()函数中。

更多相关细节:



使用模板时花费太多时间。包在golang中为客户端生成一个动态网页


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("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
}

and html template test.html.

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

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

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

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

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

解决方案

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: "./templates/test.html" 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:

It takes too much time when using "template" package to generate a dynamic web page to client in golang

这篇关于如何使用go模板来解析带有FuncMap的html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆