douan4347 2018-11-09 00:33
浏览 96
已采纳

在{{template}}中使用Go Template var

In Go we can create a variable easily enough with

{{- if .Bool.Var -}}
  {{ $MyVar := "val" }}
{{- end -}}

We can even create shared snippets easily enough

{{- define "val" -}}
  <p>Some shared template data</p>
{{- end -}}

My question is, how do we go about using $MyVar as a text value for {{template}} so that we can do something like {{template $MyVar}}, without causing an error, or is this not possible?

  • 写回答

1条回答 默认 最新

  • douyanning3724 2018-11-09 02:00
    关注

    In the pure Go text/template language (which is not Go per se but something different which happens to be implemented in Go) this isn't possible; the {{template}} invocation takes a literal string name.

    One of the most prominent users of this language is the Kubernetes Helm deployment manager. That includes several extensions to the template language. One of those is an include template function that can take any value as the name of the template, and produces a string rather than immediately outputting the template contents (you can include it in a pipeline, which you can't with template). So specifically in a kubernetes-helm YAML file, you could

    {{ include $MyVar }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 问题重发,R语言:代码运行过程中出现如下警告,请求如何解决!
  • ¥15 苍穹外卖拦截器token为null
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响