Skip to curriculum
Jump to the editor

23.2. A template engine with {{ }}

Medium
Topics
15–30 min

Email bodies, notification text, server-rendered snippets: many apps fill a fixed piece of HTML with values, and a tiny template engine does it. Hi {{ name }} plus { name: "Asha" } gives Hi Asha. The danger is the value. A name like <img src=x onerror=…> must show up as text, not become a tag that runs code, so a safe engine escapes every value unless the template asks otherwise.

Write render(template, data) in template.js.

Signature

JAVASCRIPT
function render(template, data) // returns a string
  • template: a string with tags. {{ path }} inserts the value at path, HTML-escaped. {{{ path }}} inserts it as it is, for HTML you already trust.
  • path: one or more names joined by dots, such as user.name or items.0. A name is made of letters, digits, _ and $. Spaces around the path are ignored.
  • data: an object with the values. It is never changed.

Requirements

  1. Text outside tags is copied unchanged, and the template's own text is never escaped. Only {{ starts a tag; a lone {, } or }} is plain text.
  2. A path is followed one name at a time through objects and arrays (items.0 is the first item), using the object's own properties only.
  3. A missing value, null or undefined renders as an empty string. Every other value renders as String(value), so 0 and false appear.
  4. {{ }} output is escaped: & becomes &amp;, < becomes &lt;, > becomes &gt;, " becomes &quot;, and ' becomes &#39;. Nothing is escaped twice.
  5. {{{ }}} output is not escaped.
  6. The template is read once, from left to right: a value that contains {{ … }} is printed, not rendered again. Values and text that contain $& or $1 are copied exactly.
  7. A tag that is never closed ({{ without }}, or {{{ without }}}) throws a SyntaxError. So does an empty or invalid path, such as {{ }}, {{ first name }} or {{ a..b }}.

Examples

JAVASCRIPT
render('<p title="{{ title }}">{{ user.name }}</p>', {
  title: "it's",
  user: { name: "Tom & <b>Jerry</b>" },
});
// '<p title="it&#39;s">Tom &amp; &lt;b&gt;Jerry&lt;/b&gt;</p>'
JAVASCRIPT
render("{{ items.1 }} / {{ missing.key }} / {{ count }}", { items: ["a", "b"], count: 0 });
// "b /  / 0"
JAVASCRIPT
render("<div>{{{ body }}}</div>", { body: "<em>trusted</em>" });
// "<div><em>trusted</em></div>"

render("Hi {{ name", {}); // throws SyntaxError

Environment

  • Plain JavaScript in a browser page; the tests call the global render from template.js.
  • Blocks such as if and each are not part of this problem (they are problem 23.3). Neither are helpers or filters.
  • Any built-in is allowed. If you use String.prototype.replace with a string replacement, remember that $ is special there.

Hints

  1. What has to be escaped first, so that the & in &lt; is not escaped again?
  2. "constructor" in {} is true. How do you stop a template from printing a function from Object.prototype?
  3. A scanning loop with indexOf("{{", position) gives you the text before each tag for free. Where does the next scan start?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…