Template Engine
JS Functions
Medium
30 min
strings
regex
parsing
security
Implement a small Mustache-style template engine.
compile(template)(named export) parses the template once and returns a function(data, options?) => string.render(template, data, options?)(default export) is shorthand forcompile(template)(data, options).
const greet = compile('<p>Hi {{ user.name | capitalize }}, you have {{count}} new {{ noun }}.</p>');
greet(
{ user: { name: 'ada' }, count: 0, noun: 'messages' },
{ filters: { capitalize: (s) => s[0].toUpperCase() + s.slice(1) } }
);
// '<p>Hi Ada, you have 0 new messages.</p>'
render('{{ html }} vs {{{ html }}}', { html: '<b>&</b>' });
// '<b>&</b> vs <b>&</b>'
Syntax
| Tag | Meaning |
|---|---|
{{ expr }} | Evaluate expr, convert to a string and HTML-escape it. |
{{{ expr }}} | Same, but not escaped (raw HTML). |
Whitespace inside the braces is ignored: {{name}} and {{ name }} are the same.
An expr is a dotted path, optionally followed by filters: path | filterA | filterB.
- Paths are resolved from
data:user.address.city. If any step isnullorundefined, the result isundefined. - Each filter name refers to
options.filters[name], a function that receives the current value and returns a new one. Filters run left to right. An unknown filter throws anErrornaming it. - After filters,
nullandundefinedrender as''. Everything else usesString(value)(so0andfalserender as"0"and"false").
Escaping
& → &, < → <, > → >, " → ", ' → '.
Requirements
- Text outside tags is copied unchanged.
- The compiled function can be called many times with different data.
- Never use
evalornew Function.
Follow-up
- Why is "escape by default, opt into raw" the right design?
- Escaping HTML is not enough inside an
hrefor a<script>. Why not? - How would you add
{{#each items}}...{{/each}}blocks?
Spotted a mistake or a rights concern? Report an issue