A Mini Template Engine

medium
Unit 16 · Strings IV0/3 to milestone

Build a tiny template renderer, like a much smaller Jinja.

Implement render(template, context) supporting:

  • {{ name }}: insert a value. Dotted paths like {{ user.name }} look up dict keys. Missing values raise KeyError. Whitespace inside the braces is optional.
  • Filters: {{ name | upper }}, | lower, | title and | default:text (used when the value is missing or None). Filters can be chained left to right.
  • Escaping: inserted values are HTML-escaped (html.escape) unless the last filter is | safe.
  • {% for item in items %}...{% endfor %}: repeat the block once per item, with item added to the context. Loops can be nested.
#strings#parsing#regex2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
A Mini Template Engine · TechGradify