Go语言是否具有模板语言?

huangapple go评论69阅读模式
英文:

Does Go feature a template language?

问题

在Puppet中,可以使用ERBs在文件中查找变量,例如:

<% @values.each  do |val| -%>
Some stuff with <%= val %>
<% end  -%>

如何在Go中实现相同的功能?

英文:

In Puppet it is possible to lookup variables in files using ERBs, e.g.:

> <% @values.each do |val| -%>
> Some stuff with <%= val %>
> <% end -%>

How to do the same using Go?

答案1

得分: 3

问题有点不清楚,但是Go语言内置了一个非常强大的模板引擎:https://golang.org/pkg/text/template/

甚至还有一个专门的HTML扩展包,可以根据上下文(属性、标签、文本等)进行自动转义:https://golang.org/pkg/html/template/

上面的示例代码看起来会是这样的:

{{ range .Values }}
    一些与 {{ . }} 相关的内容
{{ end }}
英文:

The question is a bit unclear, but Go has a very powerful template engine built in: https://golang.org/pkg/text/template/

There's even a special HTML extension package of it that does automatic escaping based on context (attributes, tags, text, etc) - https://golang.org/pkg/html/template/

The above example would look something like:

{{ range .Values }}
    Some stuff with {{ . }}
{{ end }}

huangapple
  • 本文由 发表于 2015年12月27日 20:09:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/34480242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定