Skip to content

Amuject/Express-Template-Engine

Repository files navigation

Express Template Engine

Easy HTML-based template engine for Express

You can see tutorial here.

Installation

npm i express-template-engine

Setup with Express

import engine from 'express-template-engine'; // ESM, Typescript
const engine = require('express-template-engine'); // CJS

// Set express template engine
app.engine('html', engine({ caching: false }));
app.set('view engine', 'html');

// Set views directory
app.set('views', path.resolve(__dirname, './views'));

// Use template engine
app.get('/', (req, res) => {
  res.render('template', { ... });
});

Function Tags

<eval> Tag

<eval>val</eval>
<eval>javascript.code({});</eval>

Variable Injection (Same as <eval> Tag)

<span>#{val}</span>

<repeat> Tag

<repeat index="i" from="0" to="5">
  <p>Run #{i} times.</p>
</repeat>
<repeat index="i" times="5">
  <p>Run <eval>i</eval> times.</p>
</repeat>

<if>, <elif>, <else> Tag

<if condition="false">
  <p>Content of IF</p>
</if>
<elif condition="val == 'cond'">
  <p>Content of ELIF</p>
</elif>
<else>
  <p>Content of ELSE</p>
</else>

<import> Tag

<import src="another/template"></import>