Conversation
seb-jean
force-pushed
the
feature/block-export-data
branch
5 times, most recently
from
September 9, 2026 20:28
ddf4a73 to
4b0964d
Compare
These two functions allow blocks to export structured data alongside
their rendered HTML output, enabling use cases where the data built
inside a block is needed as a hash rather than as a string.
block_export(data) stores data on CoreExtension via a side-channel.
block_data(name) renders the block (discarding HTML) and returns
the exported data — or an empty array if no export was made.
This is particularly useful for Twig Component form themes where
the spread syntax ({{ ...hash }}) requires an associative array
instead of a rendered HTML attribute string.
See twigphp#4919
seb-jean
force-pushed
the
feature/block-export-data
branch
from
September 14, 2026 15:53
4b0964d to
db70e37
Compare
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.
Summary
This PR adds two new Twig functions that allow blocks to export structured data alongside their rendered HTML output:
block_export(data): called inside a block to store structured data onCoreExtensionvia a side-channel. The block continues to render HTML normally.block_data(name): renders the block (HTML output is discarded) and returns the data exported viablock_export(). Returns[]if the block does not callblock_export().Use case
Twig Components (
<twig:>syntax) use the spread syntax ({{ ...hash }}) which requires an associative array — not an HTML string. Today, blocks likewidget_attributesin Symfony's form themes render HTML attribute strings, making them incompatible with the<twig:>parser.With
block_data()/block_export(), a Symfony form theme block can export its attributes as a hash while still rendering HTML for backward compatibility:{% block attributes %} {%- set attrs = {} -%} {%- for attrname, attrvalue in attr -%} {%- if attrvalue is same as(true) -%} {%- set attrs = attrs|merge({(attrname): attrname}) -%} {%- elseif attrvalue is not same as(false) -%} {%- set attrs = attrs|merge({(attrname): attrvalue}) -%} {%- endif -%} {%- endfor -%} {%- do block_export(attrs) -%} {%- for attrname, attrvalue in attrs -%} {{- ' ' ~ attrname }}="{{ attrvalue }}" {%- endfor -%} {% endblock %}A Twig Component form theme can then use:
Implementation
block_data()uses aparser_callableto compile to aBlockDataExpressionnode (mirrors theblock()/BlockReferenceExpressionpattern)BlockDataExpressioncompiles to$this->unwrap()->renderBlockData(...)Template::renderBlockData()callsrenderBlock()then returns the exported data fromCoreExtension::getExportedBlockData()null), preventing leaks between callsTests
4 test fixtures covering: basic usage, block override, block parent, and missing export fallback.
Closes #4919
Related: symfony/symfony#65713