Skip to content

Add variables, loop positions, ? :, whitespace trimming, byte output and template objects - #14

Merged
mierau merged 3 commits into
mainfrom
claude/project-thread-ao0e3p
Oct 12, 2026
Merged

mierau merged 3 commits into
mainfrom
claude/project-thread-ao0e3p

Conversation

@mierau

@mierau mierau commented Oct 12, 2026

Copy link
Copy Markdown
Owner

Before: templates had no way to name a computed value, tell the first or last loop element apart, choose between two values inline, or trim whitespace next to a tag. Servers had to turn rendered Strings back into bytes, and every context had to be converted into TemplateValues up front.

After:

  • {% set total = order.items.@sum.price %} stores a variable. Setting one that already exists changes it, so a loop can add to a total set before the loop. A variable first set inside a loop goes out of scope when the loop ends. One set inside an if stays in scope after it.
  • Loops add itemIsFirst and itemIsLast alongside itemIndex.
  • condition ? then : otherwise works anywhere an expression does, and only the side the condition picks is evaluated, in strict mode too.
  • {%- and -%} trim the whitespace before or after a tag, line breaks included.
  • render(_:features:bytes:) hands over UTF-8 bytes in chunks and passes errors from the write closure on to the caller.
  • TemplateObject lets a value give the template one key at a time, as TemplateValue.object(_:). On Apple platforms, TemplateValue(any:) and render(object:) read other NSObjects, such as Core Data objects, the same way through their @objc properties. Before, these rendered only as their description.

TemplateValue gains a case, so code that switches over it exhaustively needs a .object branch. Its == and hash(into:) are now written out by hand with the same meaning as before.

How: set compiles to a new set instruction that writes a variable slot. Slots are no longer reused, so a variable not yet set reads as nil. Loops take four slots instead of two. The conditional is a new syntax node and operation. Value operations read an object's keys through value(forKey:), and use its templateValue wherever the whole value is needed.

I also tried having render(object:) read dictionaries lazily. On the list benchmark it was about 1.7x slower, even with a per-key cache, so render(object:) still converts dictionaries up front. The benchmarks now include a render(object:) row, and rendering speed is the same as main within noise.

The new tests pass on Linux with Swift 6.0.3 (174 tests). The KVC test only runs on macOS, so CI is the first place it runs.


Generated by Claude Code

@mierau mierau self-assigned this Oct 12, 2026
@mierau
mierau merged commit 43d2851 into main Oct 12, 2026
2 checks passed
@mierau
mierau deleted the claude/project-thread-ao0e3p branch October 12, 2026 16:15
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