Deviopath › Node.js

Mustache-Lite Template Engine

Hard · Templates

Express renders HTML with template engines such as Mustache or Handlebars. Write render(template, view), a small mustache-like engine.

Tags:

- {{name}} inserts a value, HTML-escaped: & → &amp;, < → &lt;, > → &gt;, " → &quot;, ' → &#39;. Escaping prevents cross-site scripting (XSS) when a value comes from a user.

- {{{name}}} inserts the value without escaping.

- {{#name}}...{{/name}} is a section. If the value is an array, render the inside once per item, with that item as the new context (an empty array renders nothing). If it is any other truthy value, render the inside once with that value as the new context. If it is falsy (false, null, undefined, 0, ""), render nothing.

- {{^name}}...{{/name}} is an inverted section: render the inside once (context unchanged) if the value is falsy or an empty array.

- {{! comment }} renders nothing.

Spaces inside tags are ignored ({{ name }}). Sections can be nested, even with the same name. Templates are always well-formed. Whitespace and newlines around tags are kept as they are.

Names are looked up in a context stack that starts as [view]; each section pushes its context.

- . is the current (top) context itself, handy for arrays of strings.

- For a plain name, search from the top of the stack down and use the first context that is an object with that key as an own property. If none has it, the value is missing.

- For a dotted name a.b.c, only a is searched that way, then .b.c is followed from that value (own properties only). If something along the way is missing, the result is missing; other contexts are not searched again.

- Missing, null and undefined render as an empty string. Other values are converted with String(value).

Next: Background Job Queue With Retries