
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
Generate HTML (fragments) with Templates that are valid HTML (fragments).
Dependencies:
Most templating languages doesn't ensure that the templates are valid HTML. Templates needs to be parsable for build tools like assetgraph-builder to able to 1. find assets and relations 2. Translate text with their data-i18n syntax.
For example consider this Mustache template: <div {{attributes}}></div>.
This looks sane, but is unfortunately not parsable by most HTML parsers.
Here is another example: <div style="{{style}}"></div>. Even though this is parsable, the text inside the style attribute is not valid CSS syntax and some parsers (I think jsdom) may throw an error.
Render template as HTML string:
(new Htmlizer('<template string>')).toString(dataObject);
Render template as DocumentFragment:
(new Htmlizer('<template string>')).toDocumentFragment(dataObject);
Syntax is similar to KnockoutJS (in fact supports a subset of Knockout templates).
Template: <span data-bind="text: mytext"></span>
Data: {mytext: 'test'}
Output: <span>test</span>
Template: <span data-bind="text: mytext, attr: {class: cls}"></span>
Data: {mytext: 'test', cls: 'btn btn-default'}
Output: <span class="btn btn-default">test</span>
Template:
<div data-bind="if: show">
This message won't be shown.
</div>
Data: {show: false}
Output: <div></div>
Template:
<div>
<!-- ko if: count -->
<div id="results"></div>
<!-- /ko -->
<!-- hz if: !count -->
No results to display.
<!-- /hz -->
</div>
Data: {count: 0}
Output: <div>No results to display.</div>
Note: You can use either "ko if:" or "hz if:" to begin an if statement. And you may either use "/ko" or "/hz" to end an if statement.
Template:
<div>
<!-- ko text: msg --><!-- /ko -->
</div>
Data: {msg: 'Hello'}
Output: <div>Hello</div>
Template:
<div data-bind="foreach: items">
<div data-bind="text: $data"></div>
</div>
Data:
{
items: ['item 1', 'item 2', 'item 3']
}
Output:
<div>
<div>item 1</div>
<div>item 2</div>
<div>item 3</div>
</div>
Template:
<div>
<!-- ko foreach: items -->
<div data-bind="text: name"></div>
<!-- /ko -->
</div>
Data:
{
items: [{name: 'item 1'}, {name: 'item 2'}, {name: 'item 3'}]
}
Output:
<div>
<div>item 1</div>
<div>item 2</div>
<div>item 3</div>
</div>
Template:
<div data-bind="html: message"></div>
Data: {message: '<b>This</b> is a <b>serious message</b>'}
Output: <div><b>This</b> is a <b>serious message</b></div>
Template:
<div data-bind="css: {warning: isWarning}"></div>
Data: {isWarning: true}
Output: <div class="warning"></div>
Template:
<div data-bind="style: {fontWeight: bold ? 'bold' : 'normal'}"></div>
Data: {bold: false}
Output: <div style="font-weight: normal;"></div>
Template:
<div data-bind="with: obj">
<span data-bind="text: val"></span>
</div>
Data: {obj: {val: 10}}
Output:
<div>
<span>10</span>
</div>
Template:
<!-- ko with: obj -->
<span data-bind="text: val"></span>
<!-- /ko -->
Data: {obj: {val: 10}}
Output: <span>10</span>
Supports all the binding contexts documented for KO 3.0 here.
Template:
<div data-bind="foreach: {data: items, as: 'obj'}">
<!-- ko foreach: subItems -->
<span data-bind="text: $element.nodeName"></span>
<span data-bind="text: obj.name"></span>
<span data-bind="text: $parent.name"></span>
<span data-bind="text: $index"></span>
<span data-bind="text: $data.name"></span>
<span data-bind="text: name"></span>
<span data-bind="text: $root === $parents[$parents.length - 1]"></span>
<!-- /ko -->
</div>
Data:
{
items: [{
name: 'item1',
subItems: [{
name: 'subitem1'
}]
}]
}
Output:
<div>
<span>SPAN</span>
<span>item1</span>
<span>item1</span>
<span>0</span>
<span>subitem1</span>
<span>subitem1</span>
<span>true</span>
</div>
To avoid conflict with KnockoutJS, set noConflict config to true:
var template = new Htmlizer('<template string>', {noConflict: true});
By default noConflict is assumed false. With noConflict = true, there are two main differences:
FAQs
HTML templates/KnockoutJS templates on node.js.
We found that htmlizer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.