node-html-templates

1.0.4 • Public • Published

node-html-templates

Simple, zero-dependency HTML templates for nodejs.

This template engine is intended to run nodejs javascript snippets while generating your your HTML. You only need to know javascript and HTML. This template engine is inspired by EJS templates.

Usage:

const {renderFile} = require('node-html-templates')(__dirname)

console.log(renderFile('./path/to/my/template.ejs'))

Template examples:

<body>
<%
   const renderer = require('./my-renderer')
   const data = require('lib/data')
   return renderer(data)
%>
</body>

Returned string is used instead of snippet code.

Return shortcut:

<%= '1234' %>

Above snippet is interpreted as <% return '1234' %> -> no need to write 'returns' for simple code

Sub-templates:

One template can include other templates via require

main.template.ejs:

<body>
  <% return require('./sub.template.ejs')({
    templateTitle: 'custom title',  // <<< template arguments are injected into its global scope
  }) %>
</body>

sub.template.ejs:

<h1>
  <%
    return `<b>${templateTitle}</b>`  // <<< templateTitle is filled with value from main.template.ejs
  %>
</h1>

See ./tests folder for more examples.

Notes:

  • For intellisense & color highlighting use EJS file extensions.
  • Each template snippet is sandboxed by node's VM interface.
  • Sub-templates inherits parent template parameters (but they can be overwritten by inline parameters)
  • Module requires:
    • './' refer to a current template directory
    • 'lib/data' resolves as ${projectRoot}/lib/data

Package Sidebar

Install

npm i node-html-templates

Weekly Downloads

6

Version

1.0.4

License

MIT

Unpacked Size

12.9 kB

Total Files

19

Last publish

Collaborators

  • vaclav-purchart