Reading — step 1 of 5
Read
~1 min readLexing & Parsing
What Template Engines Solve
A template engine takes a template (text with placeholders) + data → final string.
Template: Hello, {{ name }}! You have {{ count }} new messages.
Data: {name: "Alice", count: 5}
Output: Hello, Alice! You have 5 new messages.
Use cases:
- HTML generation (server-side rendered web pages).
- Email composition.
- Code generation (Jinja2 in Ansible templates).
- Config file rendering (Helm charts, k8s manifests).
- Documentation generation.
Real engines (target Jinja2-like for our build):
- Jinja2 (Python, used by Flask, Ansible, Salt).
- Mustache (logic-less, multi-language).
- Handlebars (JS, extends Mustache).
- EJS (JS, embedded JavaScript).
- ERB (Ruby on Rails).
- Go templates (
text/template,html/template). - Liquid (Shopify, Jekyll).
Two design philosophies:
- Logic-less (Mustache): no conditionals/loops in templates; all logic in data.
- Logic-full (Jinja2, ERB): full conditionals, loops, even arbitrary code.
Trade-off: less logic = simpler templates, but more code preparing data; more logic = templates harder to test, easier to write.
Modern web frameworks (React, Vue, Svelte) use component-based rendering, blurring the template/code boundary.
Key features we'll implement:
{{ var }}substitution.{% if %},{% for %},{% else %},{% endif %}.- Filters:
{{ name | upper }}. - Auto-escaping for HTML safety.
- Template inheritance (
{% extends %},{% block %}). - Includes (
{% include %}).
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…