Layouts compose by folder
Each segment can declare its own layout.tsx. They nest automatically: a child layout renders inside its parent. The render tree is exactly what the folder tree looks like.
app/layout.tsx → wraps everything
app/dashboard/layout.tsx → wraps /dashboard/*
app/dashboard/settings/page.tsx → rendered inside both layouts
What persists, what re-renders
Navigating from /dashboard/settings to /dashboard/billing only swaps the page; both layouts stay mounted. Navigating from /dashboard/settings to /marketing/pricing unmounts the dashboard layout and mounts whatever wraps marketing.
layout.tsx vs template.tsx
Use layout.tsx by default. Use template.tsx when you need fresh state or enter/exit animations on every navigation — it re-mounts each time. The name in the folder is the only difference; the props are the same.