htamale v1.0.0
Htamale
Easy to use tool for HTML templating and parsing.
const htamale = require('htamale');
const src = '<h2 class="title" ht-target="hello"></h2>');
const cfg = { hello: "Hello, world!" }
htamale(src, cfg)
//=> <h2 class="title">Hello, world!</h2>
Installation
npm install --save-dev htamale
API
Components
Components are defined by the type field.
Datetime Object
Mandatory | Optional | Dynamic |
---|---|---|
type : String | format : See Momentum.js | none |
There are some standard formats that are included.
Defaults | Description
--- | --- | ---
gen1
| Will output MMMM YYYY
or YYYY
if month is not given
gen2
| Will output MMM YYYY
or YYYY
if month is not given
Example
{
"type": "datetime",
"format": "YYYY-MM-DD",
"year": 2016,
"month": 10,
"date": 9
}
Interval Object
Mandatory | Optional | Dynamic |
---|---|---|
type : String | | start : Component | |
| | end : Component | |
| | delim : Component |
Example
{
"type": "interval",
"delim": " - ",
"start": { "type": "html", "tag": "p" },
"end": { "type": "html", "tag": "p" }
}
HTML Object
Mandatory | Optional | Dynamic |
---|---|---|
type : String | class : String[] | html : Component |
| `attr`: Map | |
Example
{
"type": "html",
"tag": "p",
"class": [
"foo",
"bar"
],
"attr": { "id": "text" },
"html": "Hello, world!"
}
HTML Properties
ht-target
This is the primary property to put in the markup. In the given example p
will be filled with what the configuration file indicates.
<p ht-target="foo.bar.baz"></p>
ht-append
With this property, the parser will write on top of the element instead of inside it.
<p ht-target="foo" ht-append></p>
Configuration
The configuration file .htamalerc
is written in JSON, to be used in the CLI although the module itself takes a JavaScript object.
Properties
Properties are defined as keys with either values or other tags
{
"foo": "bar",
"foo2": {
"foo3": "baz"
}
}
Arrays
Arrays will clone a given element and duplicate if appending or write duplicates with the given values.
Arrays may not contain objects with properties, only values.
{
"foo": [
"bar",
"baz"
]
}
_value
Tag
When tags and a value must be combined, such as in deep template, using the _value
tag.
{
"foo": {
"_value": [],
"bar": "baz"
}
}
CLI
The CLI can receive input from a pipe when given "-
" as the last argument or load a file. It can then output to the console, piped to a file, or saved to a file using the outfile flag.
Usage: htamale [options] <file ...>
Options:
-V, --version output the version number
-r, --replace Write output in-place, replacing input
-o, --outfile [file,...] Write output to files in order
-c, --config [file] Path to config file
-q, --quiet Suppress logging to stdout
-h, --help output usage information
7 years ago