0.1.3 • Published 5 years ago
@voliware/node-inject v0.1.3
node-inject
Inject data into another file based on tags
How to Use it
Suppose you want to inject HTML templates into a main HTML file. For example, you are building a shop page and you want to inject a shopping cart icon and an item list table.
/templates/itemlist.html
<table id="itemlist">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
</table>
/templates/shoppingcart.html
<div id="shoppingcart">
<div id="carticon"></div>
<div id="itemcount"></div>
<div id="paynow"></div>
</div>
Into a main HTML file
/templates/index.html
<!doctype html>
<html lang="en">
<head>
<title>Shop</title>
</head>
<body>
<!-- inject:itemlist -->
<!-- inject:shoppingcart -->
</body>
</html>
Just write and execute the following code
const Inject = require('@voliware/node-inject');
let inject = new Inject()
.inject("./templates/itemlist.html", 'itemlist')
.inject("./templates/shoppingcart.html", 'shoppingcart')
.into("./templates/index.html")
.saveas("./index.html")
.run();
And you'll end up with
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Shop</title>
</head>
<body>
<table id="itemlist">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
</table>
<div id="shoppingcart">
<div id="carticon"></div>
<div id="itemcount"></div>
<div id="paynow"></div>
</div>
</body>
</html>
Node
Install with node package manager
npm install @voliware/node-inject
Include in a file
const Inject = require('@voliware/node-inject');