doupu2722 2018-02-18 20:20
浏览 95
已采纳

使用golang将html模板存储为DB中的文本字段

I'm a beginner in Go and Echo. I am required to store a html template(email template) , which will also have some details passed as context. So that it can be stored into body column (text in MySQL) and will be triggered later.

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")
            }
        }

The EmailJob

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)

this line gives error as it returns an error for render. I am not sure how I will I do it? I hope I made it clear. A little help will be much appreciated.

  • 写回答

2条回答 默认 最新

  • dongrunying7537 2018-02-18 20:44
    关注

    You are using context.Render method incorrectly.

    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
    

    The Render methods renders the template and sends it as a response. This method returns an error value, if something unexpected happens, this error value is a description of it. Otherwise, it is equal to nil. See: https://golang.org/pkg/errors/

    In order to use Renderer, you must register it and you can use that registered renderer to get rendered template text and save it in DB.

    You can see example renderer in unit tests of Echo framework: https://github.com/labstack/echo/blob/master/context_test.go#L23

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测