2.1.0 • Published 10 years ago
hyperscript-string v2.1.0
hyperscript-string
Create HTML strings with JavaScript
Goes hand in hand with hyperscript-helpers.
Installation
Install hyperscript-string using npm:
npm install --save hyperscript-stringUsage
Module usage
const h = require('hyperscript-string');
const html = h('ul.list', [
h('li.item', 'Lorem ipsum'),
h('li.item', {title: 'Second item'}, 'Dolor sit'),
h('li.item', 'Amet')
]);
/*
<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.
Related packages
hyperscript-string-async- An async/promisified version of hyperscript-stringhyperscript-helpers- Terse syntax for hyperscripthyperscript- Create HyperText with JavaScript, on client or server.
API
h(selector[, attrs, children])
| 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: String, the generated HTML.
License
MIT © Joakim Carlstein