【问题标题】:What are Parse Html token methods for email templates in golang?golang 中电子邮件模板的 Parse Html 令牌方法是什么?
【发布时间】:2017-11-10 15:37:20
【问题描述】:

我正在尝试在 golang 中创建具有 html 令牌的电子邮件模板。我在网上搜索了一遍,发现

"html/template"

图书馆。它支持如下令牌格式

Hello {{.Name}}
   <a href="{{.URL}}">Confirm email address</a>

但对 html 令牌的要求类似于

Name: {{ test.name }}
Phone: {{ test.phone }}
Address: {{ test.address }}, {{ test.city }}, {{ test.state }} {{ test.zip }}

我在 golang 或任何支持这种格式的库中找不到这样的令牌系统。谁能告诉我如何才能创建这样的令牌。属性前不应该有点。要么它应该只是像{{Name}} 或像{{ test.name }} 这样的属性。

谢谢!

【问题讨论】:

    标签: go html-parsing stringtokenizer


    【解决方案1】:

    如果您可以在属性名称前使用$,则可以使用模板的[with][1] 操作。比如:

    tmpl :=`
    {{ with $test := . }} 
    Name: {{ $test.Name }}
    Phone: {{ $test.Phone }}
    Address: {{ $test.Address }}, {{ $test.City }}, {{ $test.State }} {{ $test.Zip }}
    {{ end }}
    `
    

    注意,每个结构体字段都需要导出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      • 2011-07-04
      • 2011-01-25
      • 2010-10-17
      • 2021-10-10
      • 2014-10-28
      相关资源
      最近更新 更多