Reusable content blocks with slot-based templating for Wagtail CMS
Project description
wagtail-reusable-blocks
A Wagtail CMS extension for creating reusable content blocks that can be shared across multiple pages.
What is this?
Create content blocks once and reuse them across your Wagtail site. When you update a reusable block, all pages using it automatically reflect the changes.
Think of it like WordPress Gutenberg's "Synced Patterns" (formerly Reusable Blocks), but for Wagtail.
Key Features
- ✅ Zero-code setup - Works out of the box, no configuration required
- ✅ Searchable - Built-in search in snippet chooser modal
- ✅ Nested blocks - Reusable blocks can contain other reusable blocks
- ✅ Circular reference detection - Prevents infinite loops automatically
- ✅ Auto-generated slugs - Slugs created automatically from names
- ✅ Admin UI - Search, filter, copy, and inspect blocks
- ✅ StreamField support - RichTextBlock and RawHTMLBlock by default
- ✅ Customizable - Extend with your own block types
- ✅ Slot-based templating (v0.2.0+) - Reusable layouts with fillable slots
- ✅ Dynamic slot selection (v0.2.0+) - Auto-populated dropdown for slot IDs
Use Cases
Content Reusability (v0.1.0+)
- Headers/Footers: Create once, use on all pages
- Call-to-Action blocks: Consistent CTAs across the site
- Promotional banners: Update in one place, reflect everywhere
- Disclaimers: Legal text that needs to be consistent
- Contact forms: Reusable form blocks
Layout Reusability (v0.2.0+)
- Page templates: Two-column, three-column, hero sections
- Card grids: Product cards, team member cards, feature highlights
- Article layouts: Consistent article structure with custom content per page
- Landing page sections: Reusable section layouts with page-specific content
Installation
pip install wagtail-reusable-blocks
Add to your INSTALLED_APPS:
# settings.py
INSTALLED_APPS = [
# ...
'wagtail_reusable_blocks',
# ...
]
Run migrations:
python manage.py migrate
That's it! Reusable Blocks will now appear in your Wagtail admin under Snippets.
Quick Start
1. Create a Reusable Block
- Go to Snippets > Reusable Blocks in Wagtail admin
- Click Add Reusable Block
- Enter a name (slug is auto-generated)
- Add content using RichTextBlock or RawHTMLBlock
- Save
2. Use in Your Page Model
from wagtail.models import Page
from wagtail.fields import StreamField
from wagtail.admin.panels import FieldPanel
from wagtail_reusable_blocks.blocks import ReusableBlockChooserBlock
class HomePage(Page):
body = StreamField([
('reusable_block', ReusableBlockChooserBlock()),
# ... other blocks
], blank=True, use_json_field=True)
content_panels = Page.content_panels + [
FieldPanel('body'),
]
3. Render in Template
{% load wagtailcore_tags %}
{% for block in page.body %}
{% include_block block %}
{% endfor %}
That's it! The reusable block content will be rendered automatically.
Choosing the Right Block
wagtail-reusable-blocks provides two block types for different use cases:
ReusableBlockChooserBlock - Content Reusability (v0.1.0+)
Use when: You want to insert finished content that's shared across pages.
Example: A promotional banner that appears on multiple pages.
from wagtail_reusable_blocks.blocks import ReusableBlockChooserBlock
body = StreamField([
('reusable_block', ReusableBlockChooserBlock()),
])
Workflow:
- Create a ReusableBlock with complete content (text, images, CTAs)
- Insert it into multiple pages
- Update the block once, all pages reflect the change
Best for:
- Site-wide announcements
- Consistent call-to-action sections
- Legal disclaimers
- Contact information blocks
ReusableLayoutBlock - Layout Reusability (v0.2.0+)
Use when: You want to reuse a layout template and fill it with page-specific content.
Example: A two-column layout where the sidebar is fixed but main content varies by page.
from wagtail_reusable_blocks.blocks import ReusableLayoutBlock
body = StreamField([
('layout', ReusableLayoutBlock()),
])
Workflow:
- Create a ReusableBlock with layout HTML containing
data-slotattributes - Select the layout in your page
- Fill each slot with page-specific content
- Layout updates affect all pages, but content remains unique
Best for:
- Page templates (two-column, three-column, hero sections)
- Card grids with custom content per card
- Article layouts with consistent structure
- Landing page sections
Important: You need to include the app's URLs for slot detection to work:
# urls.py
from django.urls import path, include
urlpatterns = [
# ... other URL patterns
path('admin/reusable-blocks/', include('wagtail_reusable_blocks.urls')),
# ... wagtail URLs
]
Slot-Based Templating Tutorial
1. Create a Layout Template
Go to Snippets > Reusable Blocks and create a new block:
Name: Two Column Layout
Content: Add an HTML block:
<div class="container">
<div class="row">
<aside class="col-md-4">
<nav class="sidebar-nav">
<!-- Fixed navigation -->
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about/">About</a></li>
</ul>
</nav>
<!-- Slot for custom sidebar content -->
<div data-slot="sidebar-extra" data-slot-label="Extra Sidebar Content">
<p>Default sidebar content</p>
</div>
</aside>
<main class="col-md-8">
<!-- Slot for main content -->
<div data-slot="main" data-slot-label="Main Content">
<p>Default main content</p>
</div>
</main>
</div>
</div>
Slot attributes:
data-slot="slot-id"- Required. Unique identifier (e.g., "main", "sidebar-extra")data-slot-label="Display Name"- Optional. Human-readable label shown in admin- Child elements - Optional. Default content displayed if slot is not filled
2. Use the Layout in a Page
from wagtail.models import Page
from wagtail.fields import StreamField
from wagtail.admin.panels import FieldPanel
from wagtail_reusable_blocks.blocks import ReusableLayoutBlock
class ArticlePage(Page):
body = StreamField([
('layout', ReusableLayoutBlock()),
], use_json_field=True)
content_panels = Page.content_panels + [
FieldPanel('body'),
]
3. Fill Slots with Content
In the Wagtail admin page editor:
- Add a "Reusable Layout" block to the body
- Select "Two Column Layout" from the layout chooser
- Automatically, the available slots appear as dropdowns:
- Slot: Main Content (dropdown)
- Slot: Extra Sidebar Content (dropdown)
- Select "Main Content" and add your content:
- Rich Text: "This is my article about..."
- Image: article-image.jpg
- Select "Extra Sidebar Content" and add:
- HTML:
<div class="ad">Advertisement</div>
- HTML:
- Publish!
4. Render in Template
{% load wagtailcore_tags %}
{% for block in page.body %}
{% include_block block %}
{% endfor %}
The layout HTML is rendered with your slot content injected at the correct positions.
5. Advanced: Nesting Layouts
You can nest layouts within slots:
Outer Layout: Page wrapper with header/footer slots Inner Layout: Article layout with sidebar/main slots
ReusableLayoutBlock: "Page Wrapper"
├─ slot: "header"
│ └─ ReusableBlockChooserBlock: "Site Header"
├─ slot: "content"
│ └─ ReusableLayoutBlock: "Two Column Layout" # Nested!
│ ├─ slot: "sidebar-extra"
│ │ └─ HTML: "<div>Ads</div>"
│ └─ slot: "main"
│ └─ RichTextBlock: "Article content..."
└─ slot: "footer"
└─ ReusableBlockChooserBlock: "Site Footer"
Configuration
All settings are optional. Configure via WAGTAIL_REUSABLE_BLOCKS in your Django settings:
# settings.py
WAGTAIL_REUSABLE_BLOCKS = {
# v0.1.0 settings
'TEMPLATE': 'my_app/custom_template.html',
'REGISTER_DEFAULT_SNIPPET': True,
'MAX_NESTING_DEPTH': 5,
# v0.2.0 settings
'SLOT_ATTRIBUTE': 'data-slot',
'SLOT_LABEL_ATTRIBUTE': 'data-slot-label',
'RENDER_TIMEOUT': 5,
}
Available Settings
| Setting | Default | Description | Version |
|---|---|---|---|
TEMPLATE |
'wagtail_reusable_blocks/reusable_block.html' |
Template used to render blocks | v0.1.0+ |
REGISTER_DEFAULT_SNIPPET |
True |
Auto-register default ReusableBlock snippet | v0.1.0+ |
MAX_NESTING_DEPTH |
5 |
Maximum depth for nested reusable blocks | v0.1.0+ |
SLOT_ATTRIBUTE |
'data-slot' |
HTML attribute for slot detection | v0.2.0+ |
SLOT_LABEL_ATTRIBUTE |
'data-slot-label' |
Optional label attribute for slots | v0.2.0+ |
RENDER_TIMEOUT |
5 |
Maximum render time in seconds | v0.2.0+ |
Advanced Usage
Custom Block Types
To add more block types (images, videos, etc.), create your own model:
from wagtail.blocks import CharBlock, ImageChooserBlock
from wagtail.fields import StreamField
from wagtail.snippets.models import register_snippet
from wagtail_reusable_blocks.models import ReusableBlock
@register_snippet
class CustomReusableBlock(ReusableBlock):
content = StreamField([
('rich_text', RichTextBlock()),
('raw_html', RawHTMLBlock()),
('image', ImageChooserBlock()),
('heading', CharBlock()),
], use_json_field=True, blank=True)
class Meta(ReusableBlock.Meta):
verbose_name = "Custom Reusable Block"
Then disable the default snippet:
# settings.py
WAGTAIL_REUSABLE_BLOCKS = {
'REGISTER_DEFAULT_SNIPPET': False,
}
Nested Blocks
Reusable blocks can contain other reusable blocks:
- Create a
ReusableBlockwith your content - Create another
ReusableBlockthat references the first one - Use the second block in your pages
Note: Circular references are automatically detected and prevented. If Block A references Block B, and you try to make Block B reference Block A, you'll get a validation error.
Custom Templates
Override the default template by creating your own:
{# templates/my_app/custom_block.html #}
<div class="reusable-block">
{{ block.content }}
</div>
Then configure it:
WAGTAIL_REUSABLE_BLOCKS = {
'TEMPLATE': 'my_app/custom_block.html',
}
Or specify per-render:
block.render(template='my_app/custom_block.html')
Troubleshooting
Circular Reference Error
Error: Circular reference detected: Layout A → Layout B → Layout A
Cause: You've created a circular reference where layouts reference each other in a loop.
Solution: Remove one of the references to break the cycle. The error message shows the exact reference chain.
Example fix:
Before (circular):
Layout A → slot → Layout B → slot → Layout A ❌
After (linear):
Layout A → slot → Layout B → slot → Layout C ✅
Maximum Nesting Depth Exceeded
Warning: Maximum nesting depth of 5 exceeded
Cause: You've nested layouts deeper than the configured limit (default: 5 levels).
Solution:
- Reduce nesting depth - Simplify your layout structure
- Increase limit (not recommended beyond 10):
# settings.py WAGTAIL_REUSABLE_BLOCKS = { 'MAX_NESTING_DEPTH': 10, # Increase with caution }
- Refactor - Consider whether deep nesting is necessary
Slots Not Appearing (v0.2.0+)
Issue: Selected a layout but no slot fields appear in the editor.
Solutions:
- Ensure you've included the app's URLs in your project:
# urls.py urlpatterns = [ path('admin/reusable-blocks/', include('wagtail_reusable_blocks.urls')), ]
- Check browser console for JavaScript errors
- Verify the layout has
data-slotattributes in its HTML - Clear browser cache and reload (Cmd+Shift+R or Ctrl+Shift+R)
Slot Content Not Rendering (v0.2.0+)
Issue: Filled a slot but content doesn't appear on the page.
Solutions:
- Check that the
slot_idmatches thedata-slotattribute exactly (case-sensitive) - Verify you're using
{% include_block block %}in your template - Inspect the rendered HTML - the slot element should contain your content
- Check browser developer tools for any JavaScript errors
Slot Dropdown Shows Wrong Slots (v0.2.0+)
Issue: Slot dropdown shows slots from a different layout.
Solutions:
- This is a caching issue - refresh the page
- If persists, clear browser cache
- Check browser console for API errors
- Verify the slot detection endpoint is accessible:
/admin/reusable-blocks/blocks/{id}/slots/
Search Not Working
Issue: Created blocks don't appear in search
Solution: Run python manage.py update_index to rebuild the search index. New blocks are automatically indexed on save.
Requirements
| Python | Django | Wagtail |
|---|---|---|
| 3.10+ | 4.2, 5.1, 5.2 | 5.2, 6.4, 7.0, 7.2 |
See our CI configuration for the complete compatibility matrix (39 tested combinations).
Features by Version
v0.1.0 - MVP
- ✅ ReusableBlock model with StreamField support
- ✅ ReusableBlockChooserBlock for page integration
- ✅ Admin UI with search, filtering, and copy functionality
- ✅ Nested blocks with circular reference detection
- ✅ Auto-generated slugs
- ✅ Searchable snippet chooser
v0.2.0 - Slot System (Current)
- ✅ ReusableLayoutBlock for layout templates with fillable slots
- ✅ SlotFillBlock for injecting custom content into layout slots
- ✅ Slot detection API endpoint (
/admin/reusable-blocks/blocks/{id}/slots/) - ✅ Dynamic slot selection UI with JavaScript widget
- ✅ Automatic slot detection from HTML
data-slotattributes - ✅ Support for nested layouts (layouts within slots)
- ✅ Default content preservation for unfilled slots
- ✅ Extended circular reference detection for slot-based nesting
- ✅ Improved error messages with reference chains
v0.3.0 - Performance & Polish (Planned)
- Caching for optimized rendering
- Usage tracking ("where is this block used?")
- Revision support
Documentation
Project Links
Contributing
We welcome contributions! Please see our Contributing Guide for details.
License
BSD 3-Clause License. See LICENSE for details.
Inspiration
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wagtail_reusable_blocks-0.2.1.tar.gz.
File metadata
- Download URL: wagtail_reusable_blocks-0.2.1.tar.gz
- Upload date:
- Size: 42.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24faa1f175c9b4630af46e12559e659959b1ea9775def79c304ddb1d6626a92d
|
|
| MD5 |
3a2bbb8bad6f4bb1e86dd6e9626573a7
|
|
| BLAKE2b-256 |
d4f9d0c669297ff936acdd611c4ca1bc16aeac2d78bbcc39e650047bab0033b0
|
Provenance
The following attestation bundles were made for wagtail_reusable_blocks-0.2.1.tar.gz:
Publisher:
publish.yml on kkm-horikawa/wagtail-reusable-blocks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wagtail_reusable_blocks-0.2.1.tar.gz -
Subject digest:
24faa1f175c9b4630af46e12559e659959b1ea9775def79c304ddb1d6626a92d - Sigstore transparency entry: 745471260
- Sigstore integration time:
-
Permalink:
kkm-horikawa/wagtail-reusable-blocks@4a97f7e32bc38fcc8cd78ccdd3ec36992c695280 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/kkm-horikawa
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4a97f7e32bc38fcc8cd78ccdd3ec36992c695280 -
Trigger Event:
push
-
Statement type:
File details
Details for the file wagtail_reusable_blocks-0.2.1-py3-none-any.whl.
File metadata
- Download URL: wagtail_reusable_blocks-0.2.1-py3-none-any.whl
- Upload date:
- Size: 30.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd671f32ce36b87fd7f8581fc9a91e762ba69ce07534abb85d0c2cee01aaaba2
|
|
| MD5 |
7db5267dea9810fcddc227c3555b7677
|
|
| BLAKE2b-256 |
4756442a626eb7a13c7f93342cc4ba8dbb27574416469a1b86f61b8208e77de9
|
Provenance
The following attestation bundles were made for wagtail_reusable_blocks-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on kkm-horikawa/wagtail-reusable-blocks
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
wagtail_reusable_blocks-0.2.1-py3-none-any.whl -
Subject digest:
fd671f32ce36b87fd7f8581fc9a91e762ba69ce07534abb85d0c2cee01aaaba2 - Sigstore transparency entry: 745471288
- Sigstore integration time:
-
Permalink:
kkm-horikawa/wagtail-reusable-blocks@4a97f7e32bc38fcc8cd78ccdd3ec36992c695280 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/kkm-horikawa
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4a97f7e32bc38fcc8cd78ccdd3ec36992c695280 -
Trigger Event:
push
-
Statement type: