html-vars-replacer

1.0.3 • Public • Published

html-vars-replacer

This module replaces variables in your HTML to be able to make a more comfortable server side render

Simple example

HTML file

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>{{TITLE}}</title>
    </head>
    <body>
        <p>{{MY_SECOND_VARIABLE}}<p>
    </body>
</html>

Express app endpoint

const express = require('express');
const path = require('path');
const HTMLVarsReplacer = require('html-vars-replacer');
const app = express();

let data = {
    TITLE : "my title",
    MY_SECOND_VARIABLE : "hello world",
};

app.get('/', async function(req, res) {
    let strPathHTML = path.join(__dirname, '/index.html');
    let strHTMLRendered = await HTMLVarsReplacer(strPathHTML, data);
    res.send(strHTMLRendered);
});

Result:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>my title</title>
    </head>
    <body>
        <p>hello world<p>
    </body>
</html>

Package Sidebar

Install

npm i html-vars-replacer

Weekly Downloads

2

Version

1.0.3

License

MIT

Unpacked Size

4.49 kB

Total Files

5

Last publish

Collaborators

  • jhonnattanrivera