Support multiple root elements in x-for templates#4750
Open
calebporzio wants to merge 2 commits intomainfrom
Open
Support multiple root elements in x-for templates#4750calebporzio wants to merge 2 commits intomainfrom
calebporzio wants to merge 2 commits intomainfrom
Conversation
x-for previously used .firstElementChild to clone exactly one element per iteration, silently discarding any siblings. This made it impossible to render multiple <td> elements per iteration inside a table row — a common need with no clean workaround. When the template has multiple children, x-for now clones the full fragment and uses comment-node boundary markers to track each iteration group. Single-element templates are unchanged (no markers, no overhead). Co-Authored-By: NFSL2001 <NightFurySL2001@users.noreply.github.com> Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
|
||
| ### Limitations with `<table>` elements | ||
|
|
||
| HTML tables have strict parsing rules — a `<div>` is not valid inside `<tr>`, so the browser will strip it before Alpine runs. If you need to iterate table cells, either place multiple `<td>` elements directly in the template (as shown above) or use `<tr>` as the root element: |
There was a problem hiding this comment.
Shouldn't it be "td is not valid inside div" ? And should be "strict template parsing rule", not "strict table parsing rule"
Collaborator
Author
There was a problem hiding this comment.
unsure? it might go both ways
…nt x-for Verifies: - Morph preserves Alpine state with multi-element x-for templates - x-if as a direct child of multi-element x-for toggles correctly - Event listeners bind to correct iteration scope across siblings - 200-item list with 2 elements per iteration renders and reorders Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Situation
x-forrenders one element per iteration. Works great — until you need siblings:What happens:
<td>s in the template<td>renders per iteration — the second is silently dropped<div>— browser strips it (invalid inside<tr>)Known pain point since 2020 (#450, discussion #935).
Problem
x-for's clone logic hardcodes single-element extraction:.firstElementChildgrabs one element, discards the restSolutions considered
<td>siblings inside<tr>.x-for-each)Chosen approach: auto-detect fragment mode
When
templateEl.content.children.length > 1→ switch to fragment mode with comment-node boundaries.When
=== 1→ existing code, unchanged, zero overhead.Why:
Blockclass withstartComment/endComment)_x_lookupand_x_refreshXForScopeare fully internal — no public API changes:nth-child, survive inside<tr>Based on the problem identified in #4730 by @NightFurySL2001.
🤖 Generated with Claude Code