Skip to content

Add the possibility to pass view blocks to layouts#874

Open
giuscris wants to merge 1 commit into2.xfrom
feature/pass-blocks-to-layouts
Open

Add the possibility to pass view blocks to layouts#874
giuscris wants to merge 1 commit into2.xfrom
feature/pass-blocks-to-layouts

Conversation

@giuscris
Copy link
Member

@giuscris giuscris commented Mar 8, 2026

This pull request adds a new method to the View class 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:

  • Added a defined(string $block): bool method to the View class, allowing templates to check if a specific block is defined during rendering. This method throws a RenderingException if called outside of the rendering context.

Improvements to layout rendering:

  • Updated the output() method to copy the current view's blocks to the layout before rendering, ensuring that block definitions are available in the layout template.

@giuscris giuscris added this to the 2.4.0 milestone Mar 8, 2026
@giuscris giuscris requested a review from Copilot March 8, 2026 17:48
@giuscris giuscris self-assigned this Mar 8, 2026
@giuscris giuscris added the enhancement New feature or request label Mar 8, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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): bool to allow templates (including layouts) to conditionally render content based on whether a block was defined.
  • Updated View::output() to propagate the current view’s $blocks into the layout before rendering it.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 311 to +314
if (isset($this->layout)) {
$this->layout->vars = $this->vars;
$this->layout->blocks = $this->blocks;

Copy link

Copilot AI Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants