@transomjs/transom-ejs-template

A TransomJS module to provide EJS templating for email and html

Usage no npm install needed!

<script type="module">
  import transomjsTransomEjsTemplate from 'https://cdn.skypack.dev/@transomjs/transom-ejs-template';
</script>

README

transom-ejs-template

Add simple EJS templating functions to your server-side Transomjs code.

Build Status Coverage Status

Installation

$ npm install --save @transomjs/transom-ejs-template

Usage

Transom-ejs-template is a module that provides a really simple way to send HTML (or text) formatted emails or render simple web pages. Using all the features of EJS, you can insert data into your templates or include other ejs templates using a relative path.

After transom.initialize, an object is stored in the registry with 'transomTemplate' as the key. Each of the following methods will return a completely rendered template with all includes resolved and interpolated data.

    server.get('/about', function (req, res, next) {
        // Fetch the configured Template module from the Registry.
        const template = server.registry.get('transomTemplate');

        // Some simple data for insertion into the template.
        const data = {
            title: "My Dawg Walker App",
            date: new Date(),
            year: new Date().getFullYear()
        };
        const content = template.renderHtmlTemplate('about', data);

        // Tell the browser what type of content we're sending.
        res.setHeader('content-type', 'text/html');
        res.end(content);
        next(false);
    });

Render HTML templates

renderHtmlTemplate(templateName, data, htmlOptions)

  • templateName is the filename as found in the templatePath, without the ".ejs" extension.
  • data is a JavaScript object containing anything that might be used within the template or it's nested child templates. Several attributes are added to data each time, the include:
    • environment is set to the process.env.NODE_ENV or 'DEVELOPMENT'
    • templateName is set to the filename of the current template
  • htmlOptions.templatePath allows overriding the default path and loading a template from somewhere else.

Render Email templates

renderEmailTemplate(templateName, data, emailOptions)

  • templateName is the filename as found in the templatePath, without the ".ejs" extension.
  • data is a JavaScript object containing anything that might be used within the template or it's nested child templates. Several attributes are added to data each time, they include:
    • environment is set to the process.env.NODE_ENV or 'DEVELOPMENT'
    • templateName is set to the filename of the current template
  • emailOptions.templatePath allows overriding the default path and loading a template from somewhere else.

Testing

When running tests and looking for templates, set the process.env.NODE_ENV to TESTING to locate templates in the ../test folder.

Updating from 1.x.x

EJS templating for includes has changed between transom-ejs 1.x.x and 2.0.0.

In the version 1 release, we were able to include EJS templates with the following syntax.

<% include user/show %>

The correct syntax should be:

<%- include('user/show') %>

Need Support?

TransomJS is developed and maintained by BinaryOps Software Inc. in Canada.