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 environment. NHP awaits the result and HTML-escapes normal moustache output.
Without a callback, rendering returns a promise:
const NHP = require("nhp");
const nhp = new NHP();
const html = await nhp.render("views/page.nhp", {
title: Promise.resolve("Welcome"),
message: "Rendered on the server"
});
nhp.destroy();Error-first callbacks are also supported:
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 and for promises returned by template expressions before completing.
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.