Templating redux - #13
Conversation
- Remove mutable template-tag leftovers: - Builder.render(), _render_piece(), _render_value() - Builder.__setattr__() slot setter - trailing-underscore handling for placeholders - debug _optimize() method - Update tests to use immutable Template objects only - Update README and Template docstrings for new API - Fix benchmark unused imports and formatting
There was a problem hiding this comment.
Pull request overview
This PR introduces a new stateless, immutable Template implementation intended for a breaking 2.0 release, replacing the prior templating behavior and updating docs/tests accordingly.
Changes:
- Add
html5tagger.Templateandbuilder @ Templatesupport, with placeholder semantics based on uppercase attribute access. - Update templating tests and README examples to the new API/semantics, and remove legacy optimization test.
- Add a benchmark script and adjust Ruff configuration for fluent side-effecting attribute access.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
html5tagger/template.py |
Adds the new Template/Slot implementation that flattens builders into static fragments plus dynamic slots. |
html5tagger/builder.py |
Updates placeholder handling and adds @ Template operator support on builders. |
html5tagger/__init__.py |
Exposes Template in the public package API. |
tests/test_templating.py |
Replaces old template-variable tests with coverage for the new stateless template behavior. |
tests/test_html5tagger.py |
Removes the _optimize test corresponding to removed functionality. |
README.md |
Updates public documentation and examples for the new templating syntax and semantics. |
scripts/benchmark.py |
Adds a benchmark comparing template rendering vs building from scratch. |
pyproject.toml |
Ignores Ruff B018 to accommodate the fluent attribute-access API style. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…nstead of plain str.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…tagger into templating-redux
# Conflicts: # pyproject.toml # scripts/benchmark.py
# Conflicts: # README.md
|
This work is essentially finished. Anyone wishing to comment on it should do so promptly, before we release v2 with this. The benchmark script is extended to a heavier and more realistic page, which also widens the performance gap: Build from scratch is the old API. CSS selectors were added in v1.4 just released. Templates provide a massive speedup and perform identically well regardless of whether selectors or "from scratch" was used (typically used together). |
Add ClassesAttributeSlot so that classes=E.ClassesTag can be supplied at render time with string/list/dict/iterable class specifications, just like static classes=. Combines correctly with static class_ and CSS selector classes.
Only support the explicit Template(builder) form. Update tests, template docstring, and README examples accordingly.
A new templating implementation replacing the old one introduced in version 1.3. The templates are immutable objects instantiated at render time with dynamic content but not internally preserving such content anymore. We are running far faster than the old template system, and can now properly support
forloops for nested items (sub templates) which the old one did not.The templating is for the highest possible performance and also to ease the construction of complex documents.
This is intended for 2.0 release, as it breaks compatibility with 1.3 templates.
New Syntax
Performance
The benchmark script shows difference between no templating (built from scratch) and the new system. The old system was slower than building from scratch and not really comparable here. Python web frameworks take some 0.5ms to even Hello World. The build from scratch for a large document can reduce the maximal req/s, while if templated it hardly affects performance.