Skip to content

Add opt-in escaping, exact decimals, streaming render and a DocC catalog - #12

Merged
mierau merged 1 commit into
mainfrom
claude/project-thread-nn9aly
Oct 10, 2026
Merged

mierau merged 1 commit into
mainfrom
claude/project-thread-nn9aly

Conversation

@mierau

@mierau mierau commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Before: HTML templates had to remember | escape on every value, Decimal prices were turned into Double (so they could pick up floating-point error), long output was always built as one big String, creating a template rebuilt every date and number formatter, and there were no browsable docs.

After:

var options = TemplateOptions()
options.escaping = .html
let page = try Template("<h1>{% post.title %}</h1>{% post.body | raw %}", options: options)

var output = ""
page.render(context, into: &output)   // any TextOutputStream, written in pieces
  • Escaping (opt-in): TemplateOptions.escaping is .none by default, since templates aren't assumed to be HTML. .html escapes & < > " ' in every value tag. Escaping { ... } takes your own transform for other formats. A value tag ending in | raw is written as it is, and one ending in | escape isn't escaped twice. Template text and log tags are never escaped.
  • Exact decimals: a new TemplateValue.decimal(Decimal) case. Decimal values from Encodable types or TemplateValue(any:) stay exact through + - * /, comparisons, @sum, @avg, round/floor/ceil/abs, and number/percent/currency/format. Mixing in a Double falls back to Double. The case is indirect, so other values stay as small as before and rendering doesn't slow down.
  • Streaming: render(_:features:into:), plus the object: and encoding: variants, write to any TextOutputStream in roughly 16 KB pieces. Rendering into a String appends to it.
  • Faster template creation: formatters are made the first time a render uses them and shared by every template with the same locale, time zone and currency. The template's functions now fall back on that shared table instead of merging into a copy of it. Parsing the benchmark fixtures went from about 27 to 20 µs (example) and 25.6 to 18 µs (list). Render times are unchanged within noise: callgrind counts about 1% fewer instructions on the list benchmark.
  • DocC: DMTemplates.docc has a landing page and three articles (template syntax, functions and formatting, rendering), and builds without warnings. .spi.yml lets Swift Package Index host it.

Source compatibility: code that switches exhaustively over TemplateValue needs a .decimal case.

How: value tags record whether their last call is raw or escape (Expr.function now carries the function's name), and Program.run writes into a byte buffer that a streaming caller flushes once it reaches the chunk size.

Tests: 116 pass on Linux with Swift 6.0.3, including new suites for escaping, decimals and streaming.


Generated by Claude Code

…log.

- TemplateOptions.escaping turns on escaping for HTML (.html) or any
  format (Escaping { ... }). Value tags ending in | raw or | escape are
  written as they are.
- TemplateValue.decimal keeps Decimal values exact through arithmetic,
  @sum, @AvG, rounding and formatting. It's boxed so other values stay
  as small as before.
- render(_:into:) streams output to a TextOutputStream in 16 KB pieces.
- Formatters are made on first use and shared by templates with the same
  locale, time zone and currency, so creating a template is about 30%
  faster.
- A DocC catalog with articles on syntax, functions and rendering, and a
  .spi.yml so Swift Package Index builds the docs.
@mierau mierau self-assigned this Oct 10, 2026
@mierau
mierau merged commit 7220502 into main Oct 10, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant