Friday, June 1, 2018

EJS partials and variables

Partials and variables allows one to define templates. for e.g. if an application want to send out an email in a predefined nice html but with a dynamically populated data, a template html can be defined inside which there can be partials or variables and do the below to generate a proper output html.

var template = ejs.compile(html);
var htmlToSend = template(dataObj.htmlData);

the difference between partial and variable is subtle in terms of syntax.

<%- %> => partial
<%= %> => variable

partials allows one to specify something that is not escaped. For e.g if the intention is to include a snippet, then snippet can be used. but if the intention is to replace only the variable, variable can be used.

references:
https://coligo.io/templating-node-and-express-apps-with-ejs/

No comments:

Post a Comment