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 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件