handlebars-laboratory
v1.3.10
Published
laboratory for handlebars
Downloads
28
Readme
Handlebars Laboratory
Handlebars laboratory, service for handlebars template.
Content
Reference
Install
$ npm i handlebars-laboratory
Document
init
Before compile template, you must use init to install handlebars helper.
const handlebarsCompiler = require('handlebars-laboratory');
handlebarsCompiler.init({
RESOURCE_HOST: '', // resource url host
});
What is 'RESOURCE_HOST'?
RESOURCE_HOST is use for image url that, our data url is relative path, so we need to set a host for images resoure host to split a final url by helper 'formatUrl'. If you not need, you can set ''.
installHelpers
For install custom helpers.
const handlebarsCompiler = require('handlebars-laboratory');
const helpers = [{ name: '...', fn: (...) => {...} }];
handlebarsCompiler.installHelpers(helpers);
checkPrefabs
It will check the prefabs(partials) from the xml template. For example, the follow xml will be check and return ['my-prefab']. It can help you to init components dynamically.
<section>
{{> my-prefab}}
</section>
const handlebarsCompiler = require('handlebars-laboratory');
handlebarsCompiler.checkPrefabs(xml); // return ['my-prefab']
installPrefabs
You should install handlebars prefabs(partials) there.
const handlebarsCompiler = require('handlebars-laboratory');
const prefabs = [{ name: 'my-prefab', content: '<p>{{add a b}}</p>' }];
handlebarsCompiler.installPrefabs(prefabs);
compile
Function to compile template by data.
const xml = '';
const data = {};
const handlebarsCompiler = require('handlebars-laboratory');
const html = handlebarsCompiler.compile(xml, data);
For example:
const html = handlebarsCompiler.compile(xml, { a: 1, b: 2 });
Result Html:
<section>
<p>3</p>
</section>
BaseHelper
- array
- isArray
- toArray
- at
- includes
- indexOf
- join
- unique
- reverse
- size
- link
- find
- findIndex
- filter
- map
- date
- moment
- timestamp
- date
- enum
- format
- formatPhone
- formatUrl
- formatPrice
- logical
- or
- and
- eq
- not
- gt
- gte
- lt
- lte
- jug
- reg
- match
- number
- isNumber
- toNumber
- toFixed
- add
- minus
- multiply
- divide
- modulo
- pow
- abs
- ceil
- floor
- random
- max
- min
- object
- isObject
- keys
- values
- entries
- has
- set
- clone
- fromEntries
- string
- isString
- toString
- substring
- concat
- split
- trim
- padStart
- padEnd
- utils
- throw
- parse
- stringify