This package offers a very simple HTML template engine.
The goal of this package is to replace certain text of your HTML markup with text chosen from your Node.js backend application.
Happy coding!
const renderHTML = require("giterman")
const finalHTML = renderHTML("./index.html", {firstName: "rani", lastName: "giterman"})
$ npm i giterman
You can call the main function with whatever name you'd like. As long as you install the package and require it in your code.
The main function receives two paramaters. First one being pathFile
and second one being fields
.
- - The path to the HTML file.
- - An object documenting the text being replaced inside the HTML file. Each key stands for the field's name inside the HTML, and it's value is the text which the HTML will be replaced with.
Example HTML file (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title</title>
</head>
<body>
<div>{{firstName}} {{lastName}}</div>
</body>
</html>
Example node.js express route which will send the HTML:
const express = require("express");
const PORT = 3000
const app = express();
const http = require("http").Server(app);
const renderHTML = require("giterman");
const finalHTML = renderHTML("./index.html", {
firstName: "rani",
lastName: "giterman",
});
app.get("/", (req, res) => {
res.send(finalHTML);
});
http.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});