Install NHP as a project dependency:
npm install nhpNHP requires Node.js 20.19.0 or newer.
Create views/page.nhp:
<!doctype html>
<html>
<body>
<h1>{{title}}</h1>
<p>{{message}}</p>
</body>
</html>{{expression}} evaluates JavaScript against the render locals. NHP HTML-escapes normal moustache output.
NHP uses error-first callbacks:
const NHP = require("nhp");
const nhp = new NHP();
nhp.render("views/page.nhp", {
title: "Welcome",
message: "Rendered on the server"
}, (error, html) => {
nhp.destroy();
if (error)
throw error;
console.log(html);
});Templates are compiled asynchronously the first time they are used. render() waits for compilation before invoking its callback.
Use the bound __express() function as an Express engine:
const express = require("express");
const NHP = require("nhp");
const app = express();
const nhp = new NHP();
app.engine("nhp", nhp.__express());
app.set("views", "./views");
app.set("view engine", "nhp");
app.get("/", (request, response) => {
response.render("page", { title: "Home", message: "Hello" });
});Call nhp.destroy() during application shutdown to close file watchers for cached templates.
- Learn the available syntax in the template language guide.
- Compile static templates in build scripts with the CLI.
- See available configuration and extension hooks in the API reference.