Hi!

Is there a way to render a HTML template into a string after loading all templates with LoadHTMLGlob? Tried so search for answers but haven't really found answers if its possible with gin.

I have some email templates which I would like to send, but I need the rendered templates as a string. Is there a built in function in gin that can achieve this or I should go the plain old way and parse the templates manually with the html/template package and execute the templates? Would be cool if I didnt have to parse the templates for the emails separately.

Thanks in advance

Comment From: UniKylin

I have a smilar problem. A "content" attribute is rendered in the page template.

{{.content}}

The content attribute is an HTML code fragment which needs to be rendered according to the original HTML.

Are there any documents.

Thanks

Comment From: Ianmuhia

Am also working on a project where am send emails through a template .I encountered the same issue here is my work around. feel free to correct me. ` //go:embed verification-email.html

var f embed.FS

func HandleVerifyEmailTask(ctx context.Context, t *asynq.Task) error { var m Mail

if err := json.Unmarshal(t.Payload(), &m); err != nil {
    return err
}
log.Println(m)
email := mail.NewMSG()
email.SetFrom(m.From).AddTo(m.To).SetSubject(m.Subject)
templ, err := template.ParseFS(f, "verification-email.html")
if err != nil {
    return err
}
buffer := new(bytes.Buffer)
if err = templ.Execute(buffer, m); err != nil {
    return err
}

mailTemplate := buffer.String()
log.Println(mailTemplate)
email.SetBody(mail.TextHTML, mailTemplate)

mc := mail_client.GetMailServer()
err = email.Send(mc)

if err != nil {
    log.Println(err)
    return err
}
log.Printf(" [*] Send Welcome Email to User %s", m.To)
return nil

}`

i have embedded the html template using embed FS.

here is the html <h6 style="font-weight: 600">Verify Email </h6> <p>you forgot your password for {{.MailData.Code}} Admin. If this is true, click below to reset your password.</p> <p style="text-align: center"><a href="javascript:void(0)" style="padding: 10px; background-color: #24695c; color: #fff; display: inline-block; border-radius: 4px;font-weight:600;">Reset Password</a></p> <p>.</p> <p>Good luck! Hope it works.</p>

Comment From: lnogueir

I would like to do the same thing. @mauserzjeh did you figure out how to do that?

Comment From: mauserzjeh

@lnogueir I ended up using the standard html/template library to load the template and then just wrote its content to a string variable

Comment From: lnogueir

@lnogueir I ended up using the standard html/template library to load the template and then just wrote its content to a string variable

I guess I will have to do the same. Thanks for letting me know.

Comment From: D1360-64RC14

Same problem here.

The environment is: I have a template (A) that receives the actual data, and another template (Body) that receives the previous template. Basically, Body(A(data), some other data).

A better solution is a (Context).HTMLString(name string, obj any) string, but even some way to access the template engine (like (Context).TemplateEngine() *template.Template) would help a lot.