Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 38 ยท Projects and Practice

Project: A Template Engine

Build a small template engine with regular expressions. Tokenize text and tags, resolve dotted paths, apply filters, escape output, and add if and for blocks with a recursive parser.

Template engines such as Mustache, Jinja, Twig, Handlebars and Blade all start from the same idea: text with holes in it. A regex finds the holes, and a tiny interpreter fills them. In this project you will build one in four steps: simple replacement, dotted paths and filters, safe escaping, and finally control blocks (if and for) with a tokenizer and a recursive parser.

This is also the best example of why regex alone is not enough. Placeholders are regular. Blocks nest, and nesting needs a program around the regex, which is exactly where the sticky-flag tokenizer from the earlier lesson comes back.

flowchart LR
  T["Template string"] --> K["Tokenizer<br/>split into text and tags"]
  K --> P["Parser<br/>build a tree of nodes"]
  P --> R["Renderer<br/>walk tree with data"]
  R --> O["Output text"]

Three stages: tokens, tree, output.

Step 1: Replace placeholders

The simplest engine is one replace call. The pattern \{\{\s*(\w+)\s*\}\} matches {{ name }}, tolerating spaces inside the braces.

Node.js Step 1: simple placeholders

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Unknown keys are left as they are, which makes mistakes visible. Real engines may instead throw an error or render an empty string; decide and document.

Step 2: Dotted paths and filters

Real data is nested (user.address.city), and templates need small transformations ({{ title | upper }}). Allow a dotted path, then zero or more filters separated by pipes, each optionally with an argument.

Node.js Step 2: paths and filters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

There are two regexes. TAG finds an entire tag and captures the path and the whole filter chain as one string. FILTER is then run over just that string to split the chain into individual filters, with an optional quoted or bare argument. Two small patterns that each do one job are easier to maintain than one huge pattern with nested repeating groups (which cannot capture each repetition anyway).

Step 3: Escape by default

Template output usually ends up in HTML. If a user's name is <script>, {{ name }} must not produce a live tag. The safe design is escape by default, opt out explicitly: {{ x }} escapes, {{{ x }}} (triple braces) or a raw filter does not.

Node.js Step 3: auto-escaping and raw output

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Order inside the alternation again matters: the triple-brace form comes first, so {{{ x }}} is not read as {{ {x} }}.

Step 4: Blocks with a tokenizer and parser

Now add {% if cond %} ... {% else %} ... {% endif %} and {% for item in items %} ... {% endfor %}. Blocks nest, so a single replace cannot do it. The solution is the classic pipeline:

  1. Tokenize: split the template into text, {{ }} and {% %} tokens with one regex using a capture group in split.
  2. Parse: walk the tokens with a stack, building a tree where if and for nodes hold child lists.
  3. Render: walk the tree recursively with the data (a scope that grows inside for).
Node.js Step 4: a complete mini template engine

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Study how regex and ordinary code share the work. Regex does the lexical job: finding tags and picking apart their pieces. The stack in parse handles structure (nesting), which a regex cannot track. This split is the model for every language processor.

Design choices to discuss

  • Whitespace control. Tags on their own line leave blank lines. Jinja has {%- to trim; you could add - handling to the tokenizer.
  • Compiling. We build a tree and walk it. Production engines often compile the template to a JavaScript function once, then call it many times.
  • Sandboxing. Never evaluate arbitrary expressions with eval inside templates; limit expressions to the paths and operations you define.
  • Error messages. Add line numbers by tracking offsets in the tokenizer (the d flag or m.index help).
  • Caching. Compile each template once and cache the result by name or hash.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the split regex wrap the tag pattern in a capture group?

    With a capture group, split includes the matched separators in the result, which gives a token list of text and tags.
  2. 2. Why do templates escape output by default?

    Escape-by-default prevents cross-site scripting mistakes; raw output must be deliberate.
  3. 3. Why is a stack used when parsing blocks?

    Nesting requires tracking open blocks, which regular expressions cannot do. A stack tracks them and detects mismatched end tags.
  4. 4. In the triple-brace pattern, why is the {{{ }}} form listed before the {{ }} form?

    If the shorter form came first it would match part of the triple-brace tag and leave stray braces.

Summary

  • A template engine is tokenizing, parsing and rendering; regex handles the tokens.
  • Split with a capture group to keep delimiters; use small patterns for each tag type.
  • Escape output by default and make raw output explicit.
  • Nesting needs a stack; regex finds the pieces, code tracks the structure.
  • Report mismatched blocks with clear errors, and never eval template expressions.

Next: Regex Practice Challenges