Re: [go-nuts] Html is not parsing while parsing a template to send email

2018-05-04 Thread Jakob Borg
Like this;

https://play.golang.org/p/VVIOY7s9xQq

//jb

On 4 May 2018, at 13:32, Amandeep Kaur 
> wrote:


Hi, Jakob Borg

Thanks for ypur reply to the post. The solution you have provided can not be 
implemented on my system. I have provided a small sample of my code structure. 
Please take a look

type EmailTemplate struct{
BookingDetails string
}

type EmailRequest struct{
EmailTo  string
EmailBody  string
}

// get saved html with tokens from database
notificationTemplate, errVal := merchantDb.GetNotificationTemplate()
request := EmailRequest{
"t...@example.com",
notificationTemplate.Content,
}
templateData.BookingDetails += 
"Industry"+industry.IndustryName+""

request.EmailSend(templateData)


func (request *EmailRequest) EmailSend(notificationTemplateData 
interface{}) (bool, error) {
body, errParse := ParseTemplate(request.EmailBody, 
notificationTemplateData)
//email sending code here
}

func ParseTemplate(templateHtml string, data interface{}) (string, error) {
var body string
t, err := template.New("my_template").Parse(templateHtml)
if err != nil {
return body, err
}
buf := new(bytes.Buffer)

if err = t.Execute(buf, data); err != nil {
return body, err
}
body = buf.String()
return body, nil
}

Where templateHtml is the email body with tokens and data is the interface 
holding dynamic values for these tokens.
When I use ParseTemplate function to parse tokens as string values then it 
works fine.
But if I have to parse html in one of my tokens then it parses html as string 
and in email displays html as string.

How ever If I try to make assertion on it from interface to template.HTML, it 
gives me error:
cannot convert notificationTemplateData (type interface {}) to type 
"html/template".HTML: need type assertion
On Friday, May 4, 2018 at 2:18:51 PM UTC+5:30, Jakob Borg wrote:
Hi,

Your post is a bit confusing and I think you may be using the word "parse" in 
the opposite of it its common meaning. However, if you want to pass a HTML 
fragment through a HTML template and have it not be escaped, look at the 
template.HTML type: https://golang.org/pkg/html/template/#HTML

//jb

On 4 May 2018, at 10:42, Amandeep Kaur 
> wrote:

Hello,

I am working on a SAAS based project for which I need to send emails to 
different clients on different events.

I am using email templates which use tokens (in format {{.TOKENNAME}}) that are 
made dynamic while sending emails. Now these token are parsed by using 
"html/template" package.

following is the custom function that I have made to parse these tokens into 
email body.

func ParseTemplate(templateHtml string, data interface{}) (string, error) {
var body string
t, err := template.New("my_template").Parse(templateHtml)
if err != nil {
return body, err
}
buf := new(bytes.Buffer)

if err = t.Execute(buf, data); err != nil {
return body, err
}
body = buf.String()
return body, nil
}

Where templateHtml is the email body with tokens and data is the interface 
holding dynamic values for these tokens. When I use ParseTemplate function to 
parse tokens as string values then it works fine. But if I have to parse html 
in one of my tokens then it parses html as string and in email displays html as 
string.

Can anybody tell me what should I do to parse html in ParseTemplate function??

Thanks!

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Html is not parsing while parsing a template to send email

2018-05-04 Thread Amandeep Kaur

Hi, Jakob Borg

Thanks for ypur reply to the post. The solution you have provided can not 
be implemented on my system. I have provided a small sample of my code 
structure. Please take a look

type EmailTemplate struct{
BookingDetails string
}

type EmailRequest struct{
EmailTo  string
EmailBody  string
}

// get saved html with tokens from database
notificationTemplate, errVal := merchantDb.GetNotificationTemplate()
request := EmailRequest{
"t...@example.com", 
notificationTemplate.Content,
}
templateData.BookingDetails += 
"Industry"+industry.IndustryName+""

request.EmailSend(templateData)


func (request *EmailRequest) EmailSend(notificationTemplateData 
interface{}) (bool, error) {
body, errParse := ParseTemplate(request.EmailBody, 
notificationTemplateData)
//email sending code here 
}

func ParseTemplate(templateHtml string, data interface{}) (string, 
error) {
var body string
t, err := template.New("my_template").Parse(templateHtml)
if err != nil {
return body, err
}
buf := new(bytes.Buffer)

if err = t.Execute(buf, data); err != nil {
return body, err
}
body = buf.String()
return body, nil
}

Where templateHtml is the email body with tokens and data is the interface 
holding dynamic values for these tokens.
When I use ParseTemplate function to parse tokens as string values then it 
works fine. 
But if I have to parse html in one of my tokens then it parses html as 
string and in email displays html as string.

How ever If I try to make assertion on it from interface to template.HTML, 
it gives me error: 
cannot convert notificationTemplateData (type interface {}) to type 
"html/template".HTML: need type assertion
On Friday, May 4, 2018 at 2:18:51 PM UTC+5:30, Jakob Borg wrote:
>
> Hi, 
>
> Your post is a bit confusing and I think you may be using the word "parse" 
> in the opposite of it its common meaning. However, if you want to pass a 
> HTML fragment through a HTML template and have it not be escaped, look at 
> the template.HTML type: https://golang.org/pkg/html/template/#HTML
>
> //jb
>
> On 4 May 2018, at 10:42, Amandeep Kaur  > wrote:
>
> Hello,
>
> I am working on a SAAS based project for which I need to send emails to 
> different clients on different events. 
>
> I am using email templates which use tokens (in format {{.TOKENNAME}}) 
> that are made dynamic while sending emails. Now these token are parsed by 
> using "html/template" package. 
>
> following is the custom function that I have made to parse these tokens 
> into email body.
>
> func ParseTemplate(templateHtml string, data interface{}) (string, error) {
> var body string
> t, err := template.New("my_template").Parse(templateHtml)
> if err != nil {
> return body, err
> }
> buf := new(bytes.Buffer)
> 
> if err = t.Execute(buf, data); err != nil {
> return body, err
> }
> body = buf.String()
> return body, nil
> }
>
> Where templateHtml is the email body with tokens and data is the interface 
> holding dynamic values for these tokens. When I use ParseTemplate function 
> to parse tokens as string values then it works fine. But if I have to parse 
> html in one of my tokens then it parses html as string and in email 
> displays html as string.
>
> Can anybody tell me what should I do to parse html in ParseTemplate 
> function??
>
> Thanks!
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts...@googlegroups.com .
> For more options, visit https://groups.google.com/d/optout.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Html is not parsing while parsing a template to send email

2018-05-04 Thread Jakob Borg
Hi,

Your post is a bit confusing and I think you may be using the word "parse" in 
the opposite of it its common meaning. However, if you want to pass a HTML 
fragment through a HTML template and have it not be escaped, look at the 
template.HTML type: https://golang.org/pkg/html/template/#HTML

//jb

On 4 May 2018, at 10:42, Amandeep Kaur 
> wrote:

Hello,

I am working on a SAAS based project for which I need to send emails to 
different clients on different events.

I am using email templates which use tokens (in format {{.TOKENNAME}}) that are 
made dynamic while sending emails. Now these token are parsed by using 
"html/template" package.

following is the custom function that I have made to parse these tokens into 
email body.

func ParseTemplate(templateHtml string, data interface{}) (string, error) {
var body string
t, err := template.New("my_template").Parse(templateHtml)
if err != nil {
return body, err
}
buf := new(bytes.Buffer)

if err = t.Execute(buf, data); err != nil {
return body, err
}
body = buf.String()
return body, nil
}

Where templateHtml is the email body with tokens and data is the interface 
holding dynamic values for these tokens. When I use ParseTemplate function to 
parse tokens as string values then it works fine. But if I have to parse html 
in one of my tokens then it parses html as string and in email displays html as 
string.

Can anybody tell me what should I do to parse html in ParseTemplate function??

Thanks!

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Html is not parsing while parsing a template to send email

2018-05-04 Thread Amandeep Kaur
Hello,

I am working on a SAAS based project for which I need to send emails to 
different clients on different events. 

I am using email templates which use tokens (in format {{.TOKENNAME}}) that 
are made dynamic while sending emails. Now these token are parsed by using 
"html/template" package. 

following is the custom function that I have made to parse these tokens 
into email body.

func ParseTemplate(templateHtml string, data interface{}) (string, error) {
var body string
t, err := template.New("my_template").Parse(templateHtml)
if err != nil {
return body, err
}
buf := new(bytes.Buffer)

if err = t.Execute(buf, data); err != nil {
return body, err
}
body = buf.String()
return body, nil
}

Where templateHtml is the email body with tokens and data is the interface 
holding dynamic values for these tokens. When I use ParseTemplate function 
to parse tokens as string values then it works fine. But if I have to parse 
html in one of my tokens then it parses html as string and in email 
displays html as string.

Can anybody tell me what should I do to parse html in ParseTemplate 
function??

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.