Templating
.
- 在Hugo中,
.
類似其它程式語言的this
.
= context
safeHTML & safeCSS
safeHTML
- 在輸入的字前面加上safeHTML,代表這串字是安全的HTML,叫go不要處理這串字
EX safeHTML 原始字串
copyright = "© 2015 Jane Doe. <a href=\"https://creativecommons.org/licenses/by/4.0/\">Some rights reserved</a>."
EX 有加 safeHTML
© 2015 Jane Doe. <a href="https://creativecommons.org/licenses/by/4.0/">Some rights reserved</a>.
EX 沒加 safeHTML
<p>© 2015 Jane Doe. <a href="https://creativecommons.org/licenses by/4.0/">Some rights reserved</a>.</p>
safeCSS
- 在輸入的字前面加上safeCSS,代表這串字是安全的CSS,叫go不要處理這串字
EX 有加 safeCSS
- 假設在.md檔裡的front-matter定義
style="color: red;"
<p style="{{ .Params.style | safeCSS }}">…</p>
→ <p style="color: red;">…</p>
EX 沒加 safeCSS
- “ZgotmplZ”是一組特殊的值,代表不安全的CSS或URL內容
<p style="{{ .Params.style }}">…</p>
→ <p style="ZgotmplZ">…</p>
使用內建變數
使用自訂變數
{{ $address := "123 Main St." }}
{{ $address }}
Partial
{{ partial "header.html" . }}
Template
- 以前Template可以用來叫用Partial,現在只能用來叫用
internal templates
- internal templates: https://gohugo.io/templates/internal/
- _internal/disqus.html
- _internal/google_news.html
- _internal/google_analytics.html
- _internal/google_analytics_async.html
- _internal/opengraph.html
- _internal/pagination.html
- _internal/schema.html
- _internal/twitter_cards.html
{{ template "_internal/<TEMPLATE>.<EXTENSION>" . }}.
eq, ge, gt, le, lt, ne
程式 |
邏輯 |
eq |
== |
ne |
!= |
ge |
>= |
gt |
> |
le |
<= |
lt |
< |
if else
{{ if eq .Site.Params.menu_style "open-menu" }}
{{- partial "menu/open-menu.html" . -}}
{{ else if eq .Site.Params.menu_style "slide-menu" }}
{{- partial "menu/slide-menu.html" . -}}
{{ end }}
if(.Site.Params.menu_style == "open-menu") {
include("menu/open-menu.html", this)
}else if(.Site.Params.menu_style == "slide-menu") {
include("menu/slide-menu.html", this)
}
{{ if or (isset .Params "alt") (isset .Params "caption") }}
Caption
{{ end }}
if ( (.Param["alt"].isset()) or (.Param["caption"].isset()) ){
print(Caption)
}
with
{{ with .Param "description" }}
{{ . }}
{{ else }}
{{ .Summary }}
{{ end }}