2

My variables I'm inserting into an HTML file using the code below inserts the variables as plaintext and does not register code within my variables.

function sendEmail(){
var htmlBody = HtmlService.createTemplateFromFile('emailFormat');
var variableOne = "text";

htmlBody.example = '<p>' + variableOne + '</p>';

var email_html = htmlBody.evaluate().getContent();

  MailApp.sendEmail({
    to: "[email protected]",
    subject: "subject",
    htmlBody: email_html
  });
}

The code above makes anywhere I put <?= example ?> within the file 'emailFormat' become "<p>text</p>". The problem is it does not display it as "text" it displays "<p>text</p>" entirely. How do I get it to show register the <p>'s as code?

1
  • Show your html code Commented Aug 15, 2019 at 18:53

2 Answers 2

7

The tags <?= ?> will escape the output, thus <?= <p> ?> will "print" the string <p> and not a paragraph tag.

You can use <?!= ?> to not escape the output. Those are useful for including additional HTML and inline JavaScript, CSS files into your templates. Using the exclamation mark should fix your issue.

But in your case, try just putting the <p> tags outside <?= =?> tags like this:

<p><?= example ?></p>

That's the typical way to do it. Or if you need to switch the tags, you could use the <?!= option, but that puts HTML in your JavaScript... it's a subjective choice, but you can also control options by using the <? ?> tags like this:

Code.gs

// html refers to a template
html.type = "p";
html.content = "some paragraph content!";

template.html

<? if (type === "p") { ?>
<p> <?= content ?> </p>
<? } ?>
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

function sendEmail(){
  var v1="Hello World";
  var html='<p>' + v1 + '</p>';
  MailApp.sendEmail({to: "[email protected]", subject: "Email Test", htmlBody: html});

}

We don't really load html into html files from gs file scripts except possibly via the Google Apps Script Advanced API. We can however load html directly into the Document Object Model of a browser by using google.script.run and then it's just a matter of getting the element by id and loading it's text or innerHTML via Javascript. I don't think even scriplets actually load it into the html file but I'm not sure about that.

1 Comment

Scriptlets do load in, I use it for other variables that are just text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.