阅读 docs.gomplate.ca 上的文档,在 Gophers Slack 上的 #gomplate 频道 与开发者和社区聊天。
gomplate 是一个模板渲染器,它支持越来越多的数据源,如:JSON(包括 EJSON -- 加密的 JSON)、YAML、AWS EC2 元数据、BoltDB、Hashicorp Consul 和 Hashicorp Vault 秘密。
快来 Gophers Slack 上的 #gomplate 频道 和 GitHub 讨论区与开发者和社区聊天吧!
以下是一些关于 gomplate 如何工作的实战案例:
$ # 最基本的,gomplate 可以和环境变量一起使用...
$ echo 'Hello, {{ .Env.USER }}' | gomplate
Hello, hairyhenderson
$ # 但这有点无聊。gomplate 也有很多功能来做有用的事情
$ gomplate -i 'the answer is: {{ mul 6 7 }}'
the answer is: 42
$ # 而且,由于 gomplate 使用了 Go 的模板语法,你可以做一些有趣的事情,比如:
$ gomplate -i '{{ range seq 5 1 }}{{ . }} {{ if eq . 1 }}{{ "blastoff" | toUpper }}{{ end }}{{ end }}'
5 4 3 2 1 BLASTOFF
$ # 当你使用数据资源时,真正的乐趣就来了!
$ cat ./config.yaml
foo:
bar:
baz: qux
$ gomplate -d config=./config.yaml -i 'the value we want is: {{ (datasource "config").foo.bar.baz }}'
the value we want is: qux
$ # 数据源是由 URL 定义的,gomplate 并不只限于基于文件的数据源:
$ gomplate -d ip=https://ipinfo.io -i 'country code: {{ (ds "ip").country }}'
country code: CA
$ # 标准输入也可以作为数据源使用:
$ echo '{"cities":["London", "Johannesburg", "Windhoek"]}' | gomplate -d city=stdin:///in.json -i '{{ range (ds "city").cities }}{{.}}, {{end}}'
London, Johannesburg, Windhoek,
$ # 而这里的东西更复杂一点:
$ export CITIES='city: [London, Johannesburg, Windhoek]'
$ cat in.tmpl
{{ range $i, $city := (ds "cities").city -}}
{{ add 1 $i }}: {{ include "weather" (print $city "?0") }}
{{ end }}
$ gomplate -d 'cities=env:///CITIES?type=application/yaml' -d 'weather=https://wttr.in/?0' -H 'weather=User-Agent: curl' -f in.tmpl
1: Weather report: London
\ / Partly cloudy
_ /"".-. 4-7 °C
\_( ). ↑ 20 km/h
/(___(__) 10 km
0.0 mm
2: Weather report: Johannesburg
\ / Partly cloudy
_ /"".-. 15 °C
\_( ). ↘ 0 km/h
/(___(__) 10 km
2.2 mm
3: Weather report: Windhoek
\ / Partly cloudy
_ /"".-. 20 °C
\_( ). ↑ 6 km/h
/(___(__) 20 km
0.0 mm
阅读 docs.gomplate.ca 上的文档,并加入 GitHub 讨论!
在问题跟踪器中报告任何发现的错误。
许可证
版权所有 (c) 2016-2021 Dave Henderson
