douqin2108 2015-12-03 06:42
浏览 40
已采纳

Golang索引模板,包括

I have two templates in my project like so:

var indextemplate = template.Must(template.New("").Parse(`<!DOCTYPE html>
<form action="/compare" method="post">
<input type="date" name="from" required>
<input type="submit">
</form>`))

var comparetemplate = template.Must(template.New("").Parse("Hours since {{.From}} are {{.Duration}}"))

I don't understand how to structure the code so I have the HTML template (with a head and </html> at the end) and just include those templates into the body.

I also don't quite understand what is best practice to structure the code so that the template matches the handler. Since IIUC you need to best compile the template outside the handler.

  • 写回答

1条回答 默认 最新

  • dongtuo3795 2015-12-03 07:42
    关注

    What you should know is that a value of template.Template can be a collection of multiple templates, see its Template.Templates() method which returns this collection.

    Each template in the collection has a unique name that it can be referred to by (see Template.Name()). And there is a {{template "name" pipeline}} action, and using that you can include other templates in a template, another template which is part of the collection.

    See this example. Let's define 2 templates:

    const tmain = `<html><body>
    Some body. Now include the other template:
    {{template "content" .}}
    </body></html>
    `
    const tcontent = `I'M THE CONTENT, param passed is: {{.Param}}`
    

    As you can see, tmain includes another template named "content". You can use the Template.New() method (stressing: method, not to be confused with the func template.New() ) to create a new associated, named template that will be part of the template whose method you're calling. As a result, they can refer to each other, e.g. they can include each other.

    Let's see code that parses these 2 templates into one template.Template so they can refer to each other (error check omitted for brevity):

    t := template.Must(template.New("main").Parse(tmain))
    t.New("content").Parse(tcontent)
    
    param := struct{ Param string }{"paramvalue"}
    
    if err := t.ExecuteTemplate(os.Stdout, "main", param); err != nil {
        fmt.Println(err)
    }
    

    Output (try it on the Go Playground):

    <html><body>
    Some body. Now include the other template:
    I'M THE CONTENT, param passed is: paramvalue
    </body></html>
    

    Alternative

    Also note that if you have many and bigger templates, this becomes less readable and maintainable. You should consider saving your templates as files, and you can use template.ParseFiles() and template.ParseGlob(), both which can parse multiple files at once and they build the template collection from them, so they can refer to each other. The names of the templates will be the names of the files.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面