LoginSignup
1

More than 1 year has passed since last update.

posted at

updated at

golang template

テンプレートの定義にはdefineを使う。

{{ define "TemplateName" }}
{{ template "child" }}
{{ end }}

テンプレートの使用

{{ tempalte "TemplateName" }}

{{ define "child" }}
HTML Text
{{ end }}

追記

ginのrouterでテンプレートを読み込むと
上記のやり方だと1つのファイルの内容しか反映されない。
解決方法は

base.tmpl.html
{{ define "top" . }}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
{{ end }}

{{ define "bottom" .}}
</body>
{{ end }}
some.tmpl.html
{{ template "top"}}
<h1>Your contents.</h1>
{{ template "bottom }}

一般的に使われてる命名
layouts/
base.tmpl
header.tmpl
footer.tmpl

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
What you can do with signing up
1