Metadata-Version: 2.3
Name: mcms-layouts
Version: 0.1.0
Summary: Provides reusable page layouts built on the MCMS `RenderTree` architecture
Author: MikemathuKE
Author-email: MikemathuKE <michaelmathu6@gmail.com>
Requires-Dist: mcms-core
Requires-Dist: mcms-render
Requires-Python: >=3.13
Description-Content-Type: text/markdown

# MCMS Layouts

`mcms_layouts` provides reusable page layouts built on the MCMS `RenderTree` architecture. A layout defines the structural composition shared across multiple pages and identifies where individual page content should be inserted.

Layouts allow pages to share common structures such as headers, navigation, sidebars, footers, and content containers without duplicating those structures in every page.

## Responsibilities

- Define and manage reusable layout definitions.
- Represent each layout using a `RenderTree`.
- Identify the page-content injection point within a layout.
- Provide layouts that can be associated with multiple pages.
- Keep layout structure independent of individual page content.
- Support layout reuse without sharing mutable render-node instances between pages.

## Layout Architecture

A layout is a reusable definition containing a `RenderTree`.

The tree contains ordinary MCMS components, including structural components and an `Empty` component designated as the page-content placeholder.

```text
LayoutDefinition
├── identifier
├── name
├── description
└── RenderTree
    ├── Header
    ├── Navigation
    ├── Empty (layout_content=True)
    └── Footer
```

The layout tree defines the shared page structure. It does not contain the actual content of an individual page.

## Content Injection

Layouts use an `Empty` component to identify where page content should be injected.

The designated component is identified through its `layout_content` field:

```python
layout_content = True
```

This is an instance-level property of the `Empty` component's render node. It distinguishes the content placeholder from other uses of the same component.

During composition, the page's content tree replaces the designated placeholder in the layout tree.

### Example

A layout might contain:

```text
Layout RenderTree
├── Header
├── Navigation
├── Empty (layout_content=True)
└── Footer
```

A page might contain:

```text
Page RenderTree
├── Hero
├── About
└── Contact
```

After composition:

```text
Composed RenderTree
├── Header
├── Navigation
├── Hero
├── About
├── Contact
└── Footer
```

The resulting tree is an ordinary `RenderTree` and can be processed by the existing rendering pipeline.

## Layout Composition

The responsibility for combining layout and page trees belongs to `mcms_render`, which owns the `RenderTree` structure and its manipulation operations.

The composition process follows these steps:

1. Retrieve the selected layout's `RenderTree`.
2. Clone the layout tree to create an independent composition.
3. Locate the `Empty` render node with `layout_content=True`.
4. Clone the page's content tree.
5. Replace the placeholder with the cloned page content.
6. Return the composed `RenderTree`.

The original layout and page trees remain unchanged.

Composition must preserve the existing `RenderTree` invariants:

- Unique node identifiers.
- Valid node registry entries.
- Correct parent-child relationships.
- Consistent child ordering.
- No shared node instances between independent trees.
- No cycles.

## Relationship with Pages

A page references a layout and maintains its own content `RenderTree`.

```text
Page
├── Layout Reference
└── Content RenderTree
```

The layout remains independently managed and can be reused by multiple pages.

Changing a page's layout does not require modifying its content tree. Similarly, modifying page content does not modify the reusable layout definition.

The composed tree is produced separately from the stored layout and page definitions.

## Relationship with Templates

Layouts and templates serve different purposes:

| Concept | Responsibility |
|---|---|
| Layout | Provides reusable structural composition surrounding page content |
| Template | Provides reusable component composition that can be inserted into page content |

A template is instantiated and absorbed into a page's content tree during authoring.

A layout is associated with a page and combined with its content tree during composition.

## Architectural Integrations

| Package | Relationship |
|---|---|
| `mcms_core` | Provides shared infrastructure and exception conventions |
| `mcms_components` | Provides the components used to construct layouts, including structural components and `Empty` |
| `mcms_render` | Owns the `RenderTree` model, cloning, insertion, and layout-page composition |
| `mcms_pages` | Associates pages with layouts and maintains page content trees |
| `mcms_templates` | Provides reusable authoring-time component compositions |
| `mcms_editor` | Provides the authoring experience for creating and editing layouts |

These relationships describe architectural responsibilities. Refer to the package's `pyproject.toml` for its actual direct Python dependencies.

## Architectural Principles

### 1. Layouts are RenderTrees

A layout uses the same `RenderTree` representation as pages and templates. No separate layout-node or rendering model is introduced.

### 2. Layouts are reusable

A single layout can be associated with multiple pages without duplicating its structural definition.

### 3. Page content remains independent

The page owns its content tree, while the layout owns its structural tree.

### 4. Content injection is explicit

The `Empty` component with `layout_content=True` identifies the content injection point. Ordinary `Empty` components are not automatically treated as layout placeholders.

### 5. Composition does not mutate source trees

Layout-page composition operates on cloned trees. The original layout and page definitions remain unchanged.

### 6. The composed tree is a normal RenderTree

After composition, the resulting tree follows the same structural rules and rendering pipeline as any other MCMS RenderTree.

## Dependencies

### Architectural integrations

`mcms_render` is responsible for tree composition and manipulation. `mcms_components` provides the components used in layout definitions, while `mcms_pages` manages the association between pages and layouts.

### Python distribution dependencies

All MCMS Packages rely on django

## Development

Run the package tests from the repository root:

```bash
uv run pytest packages/mcms-layouts/tests
```

Run the repository-configured Ruff and mypy checks where applicable.

## Future Considerations

Potential future capabilities include:

- Multiple named content slots within a layout.
- Layout inheritance or nested layout structures.
- Layout-specific editor interfaces.
- Layout previews.
- Layout versioning and publishing.
- Layout assignment rules based on page type or other page metadata.
