在html / templates中,是否可以在所有页面上使用恒定的页眉/页脚?

在html / templates中,是否可以在所有页面上使用恒定的页眉/页脚?

问题描述:

Reading the docs wasn't particularly helpful and I want to know if the structure

{{header}}

   {{content that always changes}}

{{footer}}

is achievable with golang.

阅读文档并不是特别有用,我想知道结构 p>

  {{header}} 
 
 {{ 更改}} 
 
 {{footer}} 
  code>  pre> 
 
 

可以通过golang实现。 p> div>

Using text/template:

  1. Code to render it to Stdout

    t := template.Must(template.ParseFiles("main.tmpl", "head.tmpl", "foot.tmpl"))
    t.Execute(os.Stdout, nil)
    
  2. main.tmpl:

    {{template "header" .}}
    <p>main content</p>
    {{template "footer" .}}
    
  3. foot.tmpl:

    {{define "footer"}}
    <footer>This is the foot</footer>
    {{end}}
    
  4. head.tmpl:

    {{define "header"}}
    <header>This is the head</header>
    {{end}}
    

This will result in:

<header>This is the head</header>
<p>main content</p>
<footer>This is the foot</footer>

Using html/template will be extremely similar.