A template parser for emails built with NodeJS
To add email-templates to your project, download the newest release file and put it in your root directory.
Then require it at the top of your script:
const emailTemplates = require('./email-templates')To read and parse a template file use read(file, [options]):
emailTemplates.read('example.html').then(p => p.text())
.then((html) => {
console.log(html)
})
.catch((error) => {
console.error("Error: " + error)
})Arguments:
file- Path to template file. Must be absolute if no root specified.[options]- Additional options passed down to parse.root- Root path of template file.- Other options are passed down to parse
Returns:
Promise<ParseResult> - The result
To parse existing text use parse(str, [options]):
emailTemplates.parse("<h1>Hello {{global.user.name}}!</h1>").then(p => p.text())
.then((html) => {
console.log(html)
})
.catch((error) => {
console.error("Error: " + error)
})Arguments:
str- String containing template[options]- Additional optionsparseStyle- Should it convert <style> content to inline css (default:true)inlineJs- Should it parse embedded js code in{{<code>}}(default:true)jsErrorStyle- Custom style for inlineJs errors in html (example:`color:red;font-size:14px;`)
Returns:
Promise<ParseResult> - The result
ParseResult.text()- Returns plain text version of parsed htmlParseResult.colorize([tokenColors])- Returns highlighted version of parsed html (for use withconsole.log)tokenColors- Custom colors for highlighting. Use colors fromconsoleColors. (default:defaultTokenColors())
ParseResult.tokens()- Returns array of parsed html tokens