0
0

More than 5 years have passed since last update.

golang 共通のテンプレート利用

Last updated at Posted at 2018-02-20

golangで共通テンプレートを利用する方法

下記のようなHTML共通部分は別のテンプレートにしたい

header.html
<!DOCTYPE html>
<html lang="ja">
<head>
</head>
<body>

  <div>
    maincontents
  </div>

</body>
</html>

ヘッダーテンプレート、フッターテンプレート、メイン部テンプレートを分割

header.html
{{define "header"}}
<!DOCTYPE html>
<html lang="ja">
<head>
<body>
{{end}}
footer.html
{{define "footer"}}
</body>
</html>
{{end}}

{{define "header"}}・・・でテンプレートをDefineする

main.html
{{template "header" .}}
  <div>
    maincontents
  </div>
{{template "footer" .}}

メイン部でDefineしたテンプレートを呼び出し

main.go
tmpl, err := template.ParseFiles("./main.html","./header.html", "./footer.html")
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0