Add the possibility to pass view blocks to layouts#874
Conversation
There was a problem hiding this comment.
Pull request overview
Adds support for checking whether a template block exists during rendering and ensures blocks defined in a view are available to its layout, improving layout/template flexibility in Formwork’s view rendering system.
Changes:
- Added
View::defined(string $block): boolto allow templates (including layouts) to conditionally render content based on whether a block was defined. - Updated
View::output()to propagate the current view’s$blocksinto the layout before rendering it.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (isset($this->layout)) { | ||
| $this->layout->vars = $this->vars; | ||
| $this->layout->blocks = $this->blocks; | ||
|
|
There was a problem hiding this comment.
Assigning $this->layout->blocks = $this->blocks and then immediately writing $this->layout->blocks['content'] = ... forces PHP to separate/copy the whole $blocks array due to copy-on-write. If blocks can be large, consider adding the content entry before assigning blocks to the layout (or otherwise avoid writing to the layout blocks array right after the assignment) to prevent an unconditional array copy when rendering with a layout.
This pull request adds a new method to the
Viewclass and improves the handling of template blocks when rendering layouts. The main focus is on enhancing template rendering flexibility and ensuring block definitions are properly managed during layout rendering.Enhancements to template rendering:
defined(string $block): boolmethod to theViewclass, allowing templates to check if a specific block is defined during rendering. This method throws aRenderingExceptionif called outside of the rendering context.Improvements to layout rendering:
output()method to copy the current view'sblocksto the layout before rendering, ensuring that block definitions are available in the layout template.