-1

I am trying to create email templates having html tokens in golang. I have searched all over the web and found

"html/template"

library. It supports token format like below

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

But the requirement for html token is something like

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

I could not found such token system in golang or any library supporting such format. Can anyone please tell how can I achieve to create such tokens. There should be no dot before the attribue. either it should be only the attribute like {{Name}} or like {{ test.name }}.

Thank you!

MKB
  • 777
  • 1
  • 9
  • 27

1 Answers1

0

If you can use a $ before attribute names, you can use the template's [with][1] action. Something like:

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

Note that each struct field needs to be exported.

abhink
  • 8,740
  • 1
  • 36
  • 48