3

ドキュメントを読んでもあまり役に立ちませんでした。構造が

{{header}}

   {{content that always changes}}

{{footer}}

golangで実現可能です。

4

1 に答える 1

1

テキスト/テン​​プレートの使用:

  1. それを標準出力にレンダリングするコード

    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. 足.tmpl:

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

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

これにより、次のようになります。

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

html/templateの使用は非常に似ています。

于 2015-06-11T19:27:59.227 に答える