Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
23.2. A template engine with {{ }}
Medium
Topics
This question is in
23. Text processing and interpretersEmail 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
function render(template, data) // returns a stringtemplate: a string with tags.{{ path }}inserts the value atpath, HTML-escaped.{{{ path }}}inserts it as it is, for HTML you already trust.path: one or more names joined by dots, such asuser.nameoritems.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
- 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. - A path is followed one name at a time through objects and arrays (
items.0is the first item), using the object's own properties only. - A missing value,
nullorundefinedrenders as an empty string. Every other value renders asString(value), so0andfalseappear. {{ }}output is escaped:&becomes&,<becomes<,>becomes>,"becomes", and'becomes'. Nothing is escaped twice.{{{ }}}output is not escaped.- The template is read once, from left to right: a value that contains
{{ … }}is printed, not rendered again. Values and text that contain$&or$1are copied exactly. - A tag that is never closed (
{{without}}, or{{{without}}}) throws aSyntaxError. So does an empty or invalid path, such as{{ }},{{ first name }}or{{ a..b }}.
Examples
render('<p title="{{ title }}">{{ user.name }}</p>', {
title: "it's",
user: { name: "Tom & <b>Jerry</b>" },
});
// '<p title="it's">Tom & <b>Jerry</b></p>'render("{{ items.1 }} / {{ missing.key }} / {{ count }}", { items: ["a", "b"], count: 0 });
// "b / / 0"render("<div>{{{ body }}}</div>", { body: "<em>trusted</em>" });
// "<div><em>trusted</em></div>"
render("Hi {{ name", {}); // throws SyntaxErrorEnvironment
- Plain JavaScript in a browser page; the tests call the global
renderfromtemplate.js. - Blocks such as
ifandeachare 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.replacewith a string replacement, remember that$is special there.
Hints
- What has to be escaped first, so that the
&in<is not escaped again? "constructor" in {}istrue. How do you stop a template from printing a function fromObject.prototype?- 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…