Repository navigation
Render templates in a scope of their own - #174
Merged
Merged
Conversation
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
This was referenced Oct 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #156
What changed
Templates no longer run inside
SimpleView::render(). The view includes them through a closure bound to a newSimpleTemplateScope, with no class scope, so$thisin a template is aTemplateScope: escaped properties,isset()/empty(),raw()andrender_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$thisfails the view withInvalidContextProperty::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
newinSimpleView::render()through a reviewednewOnlyWhereAllowed.valueObjectsentry rather than an injected scope factory. It is howSimpleViewrenders, and replacing it means replacing the view throughViewFactory. 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.phpfail againstorigin/main'sSimpleView(Tests: 6, Assertions: 7, Failures: 6) and pass with the change.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 testandcomposer lintpass locally, and CI is green on the current head.breakingand a ruling.Anything left open
Breaking for templates that used view internals, render locals or assignments to
$this; annotations change fromViewtoTemplateScope(see "I need to print output" indocs/patterns.md). Presenter methods follow in #173.