Go – How to render the template to a temporary byte buffer using Pongo2?

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

Go - How to render the template to a temporary byte buffer using Pongo2?

问题

我正在尝试使用Golang发送HTML邮件,但不是使用原生的Golang html/template包,而是尝试使用Pongo2。

在这个问题中:https://stackoverflow.com/questions/24093200/is-it-possible-to-create-email-templates-with-css-in-google-app-engine-go

用户提供了以下示例,其中使用了html/template:

var tmpl = template.Must(template.ParseFiles("templates/email.html"))

buff := new(bytes.Buffer)
if err = tmpl.Execute(buff, struct{ Name string }{"Juliet"}); err != nil {
    panic(err.Error())
}

msg := &mail.Message{
    Sender:   "romeo@montague.com",
    To:       []string{"Juliet <juliet@capulet.org>"},
    Subject:  "See you tonight",
    Body:     "...you put here the non-HTML part...",
    HTMLBody: buff.String(),
}

c := appengine.NewContext(r)
if err := mail.Send(c, msg); err != nil {
    c.Errorf("Alas, my user, the email failed to sendeth: %v", err)
}

我想要做的是:

var tmpl = pongo2.Must(pongo2.FromFile("template.html"))
buff := new(bytes.Buffer)
tmpl.Execute(buff, pongo2.Context{"data": "best-data"}, w)

问题在于,pongo2.Execute()只允许输入上下文数据,而不是buff。

我的最终目标是能够使用Pongo2编写模板,并以一种可以用于发送电子邮件的方式呈现HTML。

我的问题是我做错了什么?我尝试实现的目标是可能的吗?如果我找到一种将HTML渲染到buff的方法,我可以在后面使用buff.String(),这将允许我将其输入到HTML正文中。

英文:

I am trying to send HTML emails using Golang, but instead of using the native Golang html/template package I am trying to do it with Pongo2.

In this question: https://stackoverflow.com/questions/24093200/is-it-possible-to-create-email-templates-with-css-in-google-app-engine-go

The user is providing this example, which is using the html/template

var tmpl = template.Must(template.ParseFiles(&quot;templates/email.html&quot;))

buff := new(bytes.Buffer)
if err = tmpl.Execute(buff, struct{ Name string }{&quot;Juliet&quot;}); err != nil {
    panic(err.Error())
}
msg := &amp;mail.Message{
    Sender:   &quot;romeo@montague.com&quot;,
    To:       []string{&quot;Juliet &lt;juliet@capulet.org&gt;&quot;},
    Subject:  &quot;See you tonight&quot;,
    Body:     &quot;...you put here the non-HTML part...&quot;,
    HTMLBody: buff.String(),
}
c := appengine.NewContext(r)
if err := mail.Send(c, msg); err != nil {
    c.Errorf(&quot;Alas, my user, the email failed to sendeth: %v&quot;, err)

What I am trying to do

var tmpl = pongo2.Must(pongo2.FromFile(&quot;template.html&quot;))
buff := new(bytes.Buffer)
tmpl.Execute(buff, pongo2.Context{&quot;data&quot;: &quot;best-data&quot;}, w)

The problem here is that pongo2.Execute() only allows to enter the context data and not the buff.

My end goal is to be able to write my templates using Pongo2, and I can render the HTML in a way where I can also use it for sending my emails.

My question is what I am doing it wrong? It's possible what I am trying to achieve? If I can find a way to render that HTML into a buff, I can use it later as part buff.String(), which will allow me to enter it in HTML body.

答案1

得分: 2

使用ExecuteWriterUnbuffered代替Execute

tmpl.ExecuteWriterUnbuffered(pongo2.Context{"data": "best-data"}, &buff)

不确定你的示例中w是做什么的。如果它是另一个你想要写入的Writer,你可以使用io.MultiWriter

// 写入w2将同时写入buff和w
w2 := io.MultiWriter(&buff, w)
英文:

Use ExecuteWriterUnbuffered instead of Execute:

tmpl.ExecuteWriterUnbuffered(pongo2.Context{&quot;data&quot;: &quot;best-data&quot;}, &amp;buff)

Not sure what w is doing in your example. If it's another Writer that you'd like to write too, you can use an io.MultiWriter.

// writes to w2 will go to both buff and w
w2 := io.MultiWriter(&amp;buff, w)

huangapple
  • 本文由 发表于 2017年1月20日 05:11:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/41751789.html
匿名

发表评论

匿名网友

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

确定