【问题标题】:Storing html template as text field in DB using golang使用golang将html模板作为文本字段存储在数据库中
【发布时间】:2018-03-14 19:32:15
【问题描述】:

我是 Go 和 Echo 的初学者。 我需要存储一个 html 模板(电子邮件模板),它还将包含一些作为上下文传递的详细信息。以便它可以存储到正文列(MySQL 中的文本)并稍后触发。

if user.Email !=""{
            visitingDetails := H{"user_name"      : user.Fname,
                                 "location"       : location.Name,
                                 "visitor_company": visitor.Company,
                                 "visitor_name"   : visitor.Fname +" "+visitor.Lname,
                                 "visitor_phone"  : visitor.Phone,
                                 "visitor_email"  : visitor.Email,
                                 "visitor_fname"  : visitor.Fname,
                                 "visitor_image"  : visitor.ProfilePicture,
                              }
            subject := visitor.Fname +" has come to visit you at the reception"
            body := c.Render(http.StatusOK,"email/user_notify_email.html",visitingDetails)
            emailJob := models.EmailJob{Recipients: visitor.Email , Subject: subject, Body: body}
            db.Create(&emailJob)
            if db.NewRecord(emailJob){
                fmt.Println("Unable to send email")
            }
        }

电子邮件工作

type EmailJob struct {
    Id              int       
    Recipients      string    
    Subject         string   
    Body            string          
    Sent            int        
    Error           string 
}

func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
    return t.templates.ExecuteTemplate(w, name, data)
}

body := c.Render(http.StatusOK,"email/user_notify_email.html",visitingDetails)

这一行给出了错误,因为它返回了渲染错误。 我不知道我会怎么做?我希望我说清楚了。非常感谢您的帮助。

【问题讨论】:

  • 错误是什么?
  • app/controllers/controllers.go:733:91: cannot use body (type error) as type string in field value ...这是我得到的错误。 @CeriseLimón
  • 您看错了行。错误出现在 emailJob 分配中。什么是c?大概 c.Render 返回一个错误,而不是渲染的模板。
  • C 是 c echo.Context 。并且 Render func 已经在细节中提到了。是的,我认为你是对的,@Peter。它可能会执行模板。

标签: html go go-echo


【解决方案1】:

你错误地使用了context.Render方法。

https://github.com/labstack/echo/blob/master/context.go#L111

// Render renders a template with data and sends a text/html response with status
// code. Renderer must be registered using `Echo.Renderer`.
Render(code int, name string, data interface{}) error

Render 方法渲染模板并将其作为响应发送。 这个方法返回一个错误值,如果发生了意外,这个错误值就是对它的描述。否则,它等于nil。 见:https://golang.org/pkg/errors/

为了使用渲染器,您必须注册它,并且您可以使用该注册的渲染器来获取渲染的模板文本并将其保存在数据库中。

您可以在 Echo 框架的单元测试中看到示例渲染器:https://github.com/labstack/echo/blob/master/context_test.go#L23

希望这会有所帮助。

【讨论】:

  • 嗨@maksadbek,感谢您查看问题。我希望该模板作为字符串值返回。相反,我得到了一个错误值。这就是问题。并且 render 是用 echo 注册的。
【解决方案2】:

在做了一些事情并理解了 golang 中的模板之后。 我想出了这样的解决方案。

t, err := template.ParseFiles("templates/email/user_notify_email.html")
        if err != nil {
            fmt.Println("Error happend")
            fmt.Println(err)
            return c.JSON(http.StatusOK, data)
        }
        buf := new(bytes.Buffer)
        if err = t.Execute(buf, visitingDetails); err != nil {
            fmt.Println(err)
        }
        body := buf.String()

现在可以存储这个身体了。 Body 已经渲染了我需要的模板。 这篇文章非常感谢https://medium.com/@dhanushgopinath/sending-html-emails-using-templates-in-golang-9e953ca32f3d

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2011-07-13
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多