docs(router): Document #[end_layout] and layout scoping behavior#628
Open
ThomasSteinbach wants to merge 1 commit intoDioxusLabs:mainfrom
Open
docs(router): Document #[end_layout] and layout scoping behavior#628ThomasSteinbach wants to merge 1 commit intoDioxusLabs:mainfrom
ThomasSteinbach wants to merge 1 commit intoDioxusLabs:mainfrom
Conversation
- Add section explaining stateful layout scoping - Document #[end_layout] attribute to close layout scopes - Add example showing correct pattern for multiple routes - Add warning example showing common nested layout mistake - Include debugging tips using cargo expand and HTML inspection - Add layout scoping rules reference Fixes DioxusLabs#627
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.
Problem
Fixes #627
The router documentation does not explain the stateful scoping behavior of
#[layout()], nor does it mention the#[end_layout]attribute. This leads to a common bug where developers accidentally create nested/duplicate layouts.Root Cause
The
#[layout()]attribute creates a persistent, stateful scope that remains active for all subsequent routes until explicitly closed with#[end_layout].When developers want multiple routes to share a layout, they naturally add
#[layout()]to each route, which creates nested layouts instead of a shared layout:Result: The
/aboutpage renders with two Wrapper components (double header, double footer).Solution
This PR adds comprehensive documentation explaining:
1. Stateful Layout Scoping
Documents that
#[layout()]creates a persistent scope affecting all subsequent routes until closed.2. The
#[end_layout]AttributeExplicitly documents
#[end_layout](analogous to#[end_nest]for nested routes).3. Correct Pattern for Multiple Routes
Shows the proper way to apply one layout to multiple routes:
4. Warning About Common Mistake
Includes a "Common Mistake" section with a working example showing the wrong pattern and explaining why it produces duplicate layouts.
5. Layout Scoping Rules
Clear bullet points explaining:
#[layout()]attributes6. Debugging Tips
Practical debugging guidance:
cargo expandto verify macro-generated nesting levels#[end_layout]to fix issuesChanges
multiple_routes: Shows correct pattern with#[end_layout]wrong_pattern: Shows common mistake with nested layoutsTesting
Both new examples include tests that verify the rendered HTML:
multiple_routes: Renders single layout correctlywrong_pattern: Demonstrates the duplicate layout bug (useful for understanding the issue)Real-World Impact
This bit us in production. Our
/tokenspage rendered with 2 complete layouts while/rendered correctly. The fix was simple (#[end_layout]), but discovering it required inspecting generated HTML, usingcargo expand, and reading Dioxus router source code.Comparison with Existing Docs
The nested routes documentation already explains this pattern for
#[nest()]and#[end_nest]. This PR brings layouts documentation to the same standard.Alternative Approaches Considered
#[end_layout]requirement: Not possible - this is core router behaviorThe documentation approach is appropriate and follows the pattern used for nested routes.