template
使用 template
函數來執行 內嵌模板。例如:
{{ range (.Paginate .Pages).Pages }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
{{ template "_internal/pagination.html" . }}
你也可以使用 template
函數來執行已定義的模板:
{{ template "foo" (dict "answer" 42) }}
{{ define "foo" }}
{{ printf "答案是 %v。" .answer }}
{{ end }}
上述範例可以用 內嵌部分模板 來重寫:
{{ partial "inline/foo.html" (dict "answer" 42) }}
{{ define "partials/inline/foo.html" }}
{{ printf "答案是 %v。" .answer }}
{{ end }}
查看 Go 的 text/template 文件以獲得更多資訊。