LoginSignup
1
1

More than 1 year has passed since last update.

golang template

Last updated at Posted at 2022-03-14

テンプレートの定義には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

1
1
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
1
1