๐ก Go text/template & html/template Syntax โ Executive Summary
Technical architectural analysis, practical configuration manifests, and deployment instructions for Go text/template & html/template Syntax.
Grammar
Conditional statement
go
{{- if $Booleans }}
{{- else if and $Booleans ( not $Booleans ) }}
{{- else }}
{{- end }}
//$Booleans1 ๋๋ $Booleans2
{{- if or $Booleans1 $Booleans2 }}
{{- end }}
Repeat statement
go
{{ range .Array }}
{{ end }} // ํด๋น ๊ฐ์ด ๋ฐฐ์ด์ผ ๊ฒฝ์ฐ foreach
Basic functions
go
// go Data๋ ์ต์์ ๋งต์ผ๋ก ๊ตฌ์ฑ๋๋ค. ๋งต์ ํค๋ก ๊ฐ ์ ๊ทผ
{{ .DATA }}
// ํ
ํ๋ฆฟ ๋ธ๋ก ์ ์ธ
{{ define "slack.main.title" }}
{{ end }}
// define๋ ํ
ํ๋ฆฟ ๋ธ๋ก ์ถ๋ ฅ
{{ template "slack.main.title" . }}
// ์ต์์ ๋ฒ์์ ์ปจํ
์คํธ(context)**๋ฅผ ์ ์ฅ. ๋ธ๋ก ๋ด์์๋ ์ฐธ์กฐ ๊ฐ๋ฅ
{{ $root := . }}
// Array
{{ index .AAA 3 }} // ๋ฐฐ์ด์ ๊ฐ ์ ๊ทผ
{{ len .Array }} // ํด๋น ๊ฐ์ด ๋ฐฐ์ด์ผ ๊ฒฝ์ฐ ๊ธธ์ด ๋ฆฌํด, String์ผ ๊ฒฝ์ฐ ๊ธ์์ ๋ฆฌํด
// Map
{{ index .AAA "B-B" "C" }} //.AAA["B-B"]["C"]
{{ .AAA.BB.C }} //.AAA.BB.C
// ๊ณต๋ฐฑ ์ ๊ฑฐ
{{ .Text -}} // ์ฐ๋ก ์กด์ ํ๋ Space(๊ณต๋ฐฑ) ์ ๊ฑฐ
{{- .Text }} // ์ข๋ก ์กด์ฌํ๋ Space(๊ณต๋ฐฑ) ์ ๊ฑฐ
// ์ฃผ์์ฒ๋ฆฌ
{{/* ์ฃผ์ */}}
String function
go
# convert String to Base64 encrypt
$String | b64enc
# ์๋ฐ์ดํ ์ฝ์
(String ํ์ผ๋ก ์ธ์๋์ด์ผํ๋ ๊ฒฝ์ฐ)
$value | quote
# ์ฝ์ด์จ Map|Array ๋ฐธ๋ฅ๋ฅผ Yaml Template ํํ๋ก ๋ณํ
toYaml $yamlValue
# ์ฃผ๋ก toYaml๊ณผ ํจ๊ป ์ฌ์ฉ๋จ. $nํ ๋ค์ฌ์ฐ๊ธฐ
$value | nindent $n
Examples
go
// 1. map
// data
{
"JSON_MAP": {
"alertname": "High CPU usage",
"team": "blue",
"zone": "eu01"
}
}
// action
{{ .JSON_MAP }}
// result
>> map[alertname:High CPU usage team:blue zone:eu-1]
// 2. array
// data
{
"JSON_ARR": [{"a":1},{"b":2},{"c":3},{"d":4}]
}
// action
{{ .JSON_ARR }}
// result
>> [map[a:1] map[b:2] map[c:3] map[d:4]]
Official document




Leave a Reply