Create HTML strings with JavaScript
Goes hand in hand with hyperscript-helpers
.
Install haitch-html
using npm:
npm install --save haitch-html
const h = require('haitch-html');
const tag = h('ul.list', [
h('li.item', 'Lorem ipsum'),
h('li.item', {title: 'Second item'}, 'Dolor sit'),
h('li.item', 'Amet')
]);
// an Haitch HTML tag object
const html = h.html(tag);
/*
<ul class="list">
<li class="item">Lorem ipsum</li>
<li title="Second item" class="item">Dolor sit</li>
<li class="item">Amet</li>
</ul>
*/
Attributes that are null
, undefined
or false
are excluded in the generated HTML.
Empty strings are left as is, e.g. h('a', {href: ''}) => '<a href=""></a>'
, and true
are generated without value, e.g. h('button', {disabled: true}, 'Click me') => '<button disabled>Click me</button>'
.
Void elements' children are excluded in the generated HTML as well.
haitch-html-async
- An async/promisified version of haitch-htmlhaitch
- Create HTML tag objects with JavaScripthyperscript-helpers
- Terse syntax for hyperscripthyperscript
- Create HyperText with JavaScript, on client or server.
Name | Type | Description |
---|---|---|
selector | String |
Contains at least the HTML tag name |
attrs | Object |
Attributes for the HTML tag |
children | String or Array |
The children for the HTML tag |
Returns: Object
, the generated HTML.
MIT © Joakim Carlstein