2.2.2 • Published 5 years ago
xhjsx v2.2.2
xHJSx
Cross HTML JavaScript (html template engine).
Installation
$ npm install xhjsx
Example
const xHJSx = require('xhjsx')
let str = `
<div>Fruits</div>
<ul>
<? fruits.forEach(e => { ?>
<li><?= e ?></li>
<? }) ?>
</ul>`
// similar echo <?= 'hello' ?> or <? xHJSx.write('hello') ?>
let output = xHJSx.render(str, {fruits: ['apple', 'pineapple']})
/* output
<div>Fruits</div>
<ul>
<li>apple</li>
<li>pineapple</li>
</ul>
*/
Usage
const xHJSx = require('xhjsx');
(async () => {
// using string
let template1 = xHJSx.compile(str) // => Function
template1(options) // => String
// using string
xHJSx.render(str, options) // => String
// using path
let template2 = await xHJSx.compilePath(path) // => Function
template2(options) // => String
// using path
await xHJSx.renderFile(path) // => String
})()
Change Config
const xHJSx = require('xhjsx')
// default config
xHJSx.allowCache = true;
xHJSx.delimiter = '?';
xHJSx.escape = '=';
xHJSx.rootDir = '.';
Include
Example file structure
.
├── index.js # main file
└── template # template root directory
├── header.html
└── index.html
header.html
<header><?= HeaderTitle ></header>
index.html
<? xHJSx.include('header.html', { HeaderTitle }) ?>
<div><?= Content ?></div>
Example script
const xHJSx = require('xhjsx');
const path = require('path');
(async () => {
// set template root directory
xHJSx.rootDir = path.join(__dirname, '/template')
let output = await xHJSx.renderFile('index.html', {HeaderTitle: 'Title', Content: 'Hello World'})
/* output
<header>Title</header>
<div>Hello World</div>
*/
})();