Parse Handlebars and HTML.
Parse this:
into this:
[ { type:"htmlTagStart" }, { type:"htmlTagNameStart" }, { type:"text", value:"tag" }, { type:"htmlTagNameEnd" }, { type:"htmlTagEnd" }, { type:"text", value:" value " }, { type:"hbsTagStart", notEscaped:true }, { type:"hbsExpressionStart" }, { type:"hbsExpressionPath", value:["obj","value"] }, { type:"hbsExpressionEnd" }, { type:"hbsTagEnd", stripWhitespace:true, notEscaped:true }, // whitespace stripped { type:"htmlTagStart", closing:true }, { type:"htmlTagNameStart" }, { type:"text", value:"tag" }, { type:"htmlTagNameEnd" }, { type:"htmlTagEnd" } ]…for use in compiling to something like a virtual DOM, such that of handlebars-react.
For more information on the strengths, weaknesses and implementation details of this library, check out the wiki.
new HandlebarsHtmlParser(options);Parse a template String and run a provided Function once per element in the generated linear program which contains HTML pieces and Handlebars expressions.
callback has two arguments:
node, the current node being processed in the linear programstate,an immutablea mutable/reused object containing the current state ofnode
Optionally, you can omit callback and use the returned value. However, you will not have access to any state.
A map of node types, especially useful with nodes in parse() callbacks. See the full list.
Example:
if (node.type === HandlebarsHtmlParser.type.HBS_EXPRESSION_START) { // is expression start node }Type: Boolean
Default value: false
When true, Handlebars comments will be converted to HTML comments.
Type: Boolean
Default value: false
When true, Handlebars comments will be excluded from output. This can result in concatenated Strings, which optimizes compilation.
Type: Boolean
Default value: false
When true, HTML comments will be excluded from output. This can result in concatenated Strings, which optimizes compilation.
Type: Boolean
Default value: false
When true, the contents of JavaScript tags, attributes and href="javascript:…" links will be minified. Note: if any Handlebars expressions are contained within, minification will be skipped.
Type: Boolean
Default value: false
When true, will replace multiple standard whitespace characters (line breaks, tabs, regular spaces) with a single space. This helps improve runtime performance and lower compiled template file size.
- Does not affect "special" whitespace chars such as
, etc. - Does not affect text within
<pre>,<script>,<style>tags - Does not affect text within "dynamic" tags (
<{{tag}}>) - Does not affect HTML's rendered appearance (unless using CSS
white-space:pre)
Type: Boolean
Default value: false
When true, XML rules will be used instead of HTML rules for parsing. This can be useful for SVG documents (that are not inlined within HTML).
-
How is this different from HTMLBars?
HTMLBars builds a DOM whereas this library allows you to build a DOM and especially a virtual DOM. -
Why not use parse5?
As of v1.5.0, it is not at all "forgiving", in that it will parse<{{tag}}>asdf</{{tag}}>as text instead of HTML.
- add support for sub-expressions
- add support for
{{#if}},{{else}},{{else if}},{{#unless}} - add support for
{{#with}},{{#each}},{{@index}},{{@key}},{{this}},{{.}},{{undefined}},{{null}},{{true}},{{1}} - add support for
{{> partial}} options.mustacheOnlythat disables helpers, expressions and whitespace control? would have to provide parse errors
- 0.0.1–0.0.14 pre-releases