Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you send HTML in the dynamic template data #738

Closed
jmichas opened this issue Sep 10, 2018 · 7 comments
Closed

Can you send HTML in the dynamic template data #738

jmichas opened this issue Sep 10, 2018 · 7 comments
Labels
difficulty: unknown or n/a fix is unknown in difficulty status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library

Comments

@jmichas
Copy link

jmichas commented Sep 10, 2018

Issue Summary

I'm currently using the workaround for sending template data to the API and it seems to be working fine. The only thing I can't figure out is how I can send an HTML fragment as a substitution.

Steps to Reproduce

So, I am using this as part of my email service:


var obj = (JObject)JsonConvert.DeserializeObject(messageJSON);
var personalizations = (JArray)obj["personalizations"];
foreach (var personalization in personalizations.Cast<JObject>())
{
	var substitutions = personalization.Property("substitutions");
	substitutions.Replace(new JProperty("dynamic_template_data", substitutions.Value));
}

var dynamicMessage = obj.ToString();

await client.RequestAsync(SendGridClient.Method.POST, dynamicMessage, null, "mail/send");

Before that I set the substitutions which will be converted to the dynamic_template_data.
If I use something like:

msg.AddSubstitution("Content","This is a test<br />test");

The {{Content}} placeholder in my template is rendered with the <br /> visible in the test string.

How can I get the template to replace the placeholder with my html content and render it as html?
(Like what the Html.Raw() method does in razor)

Thanks,
Jason

@kylearoberts
Copy link

Hi @jmichas

What you want to do is change your content from using {{content}} to {{{content}}}. We have an example of this in our docs here.

Handlebars HTML-escapes values returned by a {{expression}}. If you don't want Handlebars to escape a value, use the "triple-stash" {{{.

Please let us know if this does not work for you.

@syedhassan
Copy link

Hi @jmichas

What you want to do is change your content from using {{content}} to {{{content}}}. We have an example of this in our docs here.

Handlebars HTML-escapes values returned by a {{expression}}. If you don't want Handlebars to escape a value, use the "triple-stash" {{{.

* [Source](https://handlebarsjs.com/)(HTML Escaping)

Please let us know if this does not work for you.
@kylearoberts the documenation link is broken can you please find doc for v3 API that speaks about {{{content}}} ?

FYI the documentation on sendgrids sucks big time why not spend some effort to update it? FYI, today the doc at https://sendgrid.com/docs/api-reference/ doesn't talk about dynamic_template_data which is your v3 standard!

@ecologik
Copy link

ecologik commented Aug 3, 2020

Does the above still apply? I am not getting the html to render using the {{{ }}} in my template. Bringing over html in a substitution tag. Hi Josh and the
tag doesn't render as html

            $email->addTo(
                   "My Email", "My Name" ,
                    [
		                 "body" => "Hi!</br>Josh",
				 "image_1" => $thumb,
				 "url" => $url,
				 "subject" => $emaila['subject']
					  ],
                       0
                    ); 

@syedhassan
Copy link

@ecologik it should definitely do it if you are using the dynamic_template_data when you render the HTML

@ecologik
Copy link

ecologik commented Aug 3, 2020

@syedhassan Thanks for the response. Interesting fix... I created another text area in the template creator and attempted to {{{ }}} my replacement var in that new area. It worked. I deleted that template object and tried it again with the original template object. It worked. I do find the Template Creator to be a bit buggy and has removed some of my objects out of nowhere. This is closed. Thanks again.

@cluis13915
Copy link

cluis13915 commented Oct 23, 2024

Hey there! Is it possible to use the {{{ Hello <b>-username-</b> }}} sintax in Legacy Transactional Templates? 🙌

@cluis13915
Copy link

cluis13915 commented Oct 23, 2024

After testing, actually is not necessary to use the curly braces to render raw HTML in Legacy templates. Just put the HTML into the substitution text like this: Hello <b>-username-</b>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: unknown or n/a fix is unknown in difficulty status: waiting for feedback waiting for feedback from the submitter type: question question directed at the library
Projects
None yet
Development

No branches or pull requests

6 participants