英文:
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("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)
What I am trying to do
var tmpl = pongo2.Must(pongo2.FromFile("template.html"))
buff := new(bytes.Buffer)
tmpl.Execute(buff, pongo2.Context{"data": "best-data"}, 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{"data": "best-data"}, &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(&buff, w)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论