Render a Mustache-Style Template

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code
  • Pro

Write render(template, data), a tiny template engine. A tag is a path in double curly braces, such as {{ user.name }}, with optional spaces inside the braces. A path is one or more names made of letters, digits and underscores, joined by dots: follow them one by one from data, where a name may also be an array index such as items.0. Replace each tag with the value it finds, as text, so 0 and false show as "0" and "false"; if the value is undefined or null, or a step along the path is missing, the tag becomes "". Values in double braces are HTML-escaped: & becomes &amp;, < becomes &lt;, > becomes &gt;, a double quote becomes &quot; and an apostrophe becomes &#39;. A tag in triple braces, such as {{{ html }}}, inserts the value as it is. Text outside tags stays the same, and a value that contains braces is never rendered again as a tag.

Your code must pass

  • Replaces tags with values
  • Follows dotted paths and array indexes
  • Renders missing values as nothing
  • Shows 0 and false
  • Escapes HTML in double braces
  • Inserts triple braces unescaped
  • Never renders tags inside values