Skip to content

Render templates in a scope of their own - #174

Merged
schlessera merged 1 commit into
mainfrom
fix/156-template-scope
Oct 24, 2026
Merged

schlessera merged 1 commit into
mainfrom
fix/156-template-scope

Conversation

@schlessera

Copy link
Copy Markdown
Member

Closes #156

What changed

Templates no longer run inside SimpleView::render(). The view includes them through a closure bound to a new SimpleTemplateScope, with no class scope, so $this in a template is a TemplateScope: escaped properties, isset()/empty(), raw() and render_partial(), each handed on to the view. Context keys named like view internals (path, view_factory, debug_mode, configuration, _context_) now read as context values, render()'s locals are gone from the template, and assigning to $this fails the view with InvalidContextProperty::for_assignment() instead of planting an unescaped value for later renders.

Why this shape

Ruled on #145 (revised decision, 2026-10-24): an always-used scope, shipped ahead of the presenter work (#173). The scope is created with new in SimpleView::render() through a reviewed newOnlyWhereAllowed.valueObjects entry rather than an injected scope factory. It is how SimpleView renders, and replacing it means replacing the view through ViewFactory. A factory would have added an optional parameter to every view and factory constructor and brought back the #149 dependency.

Proof

The six new regression tests in tests/php/Unit/SimpleViewTest.php fail against origin/main's SimpleView (Tests: 6, Assertions: 7, Failures: 6) and pass with the change.

composer test      # PHP 8.3: OK (604 tests); PHP 7.4 (dependencies resolved for 7.4): OK, 5 skipped
composer lint      # phpcs, PHPStan level max, Psalm, PHPStan rule tests: all clean on PHP 8.3

The template's only variable is $template, the path being included. An independent review (gpt-6-astra) checked isolation, the documented API, buffers with assertions disabled, citations and test sensitivity. It found one real issue: the docs overstated the isolation, since reflection or rebinding closures can still reach the view. The docs now say the scope prevents accidents and is not a sandbox.

Checklist

  • composer test and composer lint pass locally, and CI is green on the current head.
  • Every acceptance criterion from the issue is met, or the gap is named below.
  • Nothing a generated plugin relies on changed, or the issue carries breaking and a ruling.
  • Docs and inline comments reflect the change, or nothing documented moved.

Anything left open

Breaking for templates that used view internals, render locals or assignments to $this; annotations change from View to TemplateScope (see "I need to print output" in docs/patterns.md). Presenter methods follow in #173.

A template ran inside SimpleView::render(), with the view's class scope and
the method's locals. Context keys named like a view property read that
property, render()'s locals were template variables, and assigning to $this
skipped escaping and outlived the render.

Templates now run in a closure bound to a SimpleTemplateScope without a class
scope. The scope offers escaped properties, isset(), raw() and
render_partial(), handing each on to the view, and refuses assignments with
InvalidContextProperty::for_assignment().

Closes #156
@schlessera schlessera added type: fix Something is broken and must behave the way it is documented. area: infrastructure src/Infrastructure: services, injector, views, autoloader. What generated plugins build on. breaking Changes an interface or convention generated plugins rely on. Needs a ruling before code. labels Oct 23, 2026
@schlessera
schlessera merged commit 92c9263 into main Oct 24, 2026
5 checks passed
@schlessera
schlessera deleted the fix/156-template-scope branch October 24, 2026 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: infrastructure src/Infrastructure: services, injector, views, autoloader. What generated plugins build on. breaking Changes an interface or convention generated plugins rely on. Needs a ruling before code. type: fix Something is broken and must behave the way it is documented.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

views: templates reach the view's internals, so some context keys are shadowed

1 participant