Skip to main content

Reusable content blocks with slot-based templating for Wagtail CMS

Project description

wagtail-reusable-blocks

PyPI version CI codecov License: BSD-3-Clause

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

Use Cases

  • 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

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

  1. Go to Snippets > Reusable Blocks in Wagtail admin
  2. Click Add Reusable Block
  3. Enter a name (slug is auto-generated)
  4. Add content using RichTextBlock or RawHTMLBlock
  5. 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.

Configuration

All settings are optional. Configure via WAGTAIL_REUSABLE_BLOCKS in your Django settings:

# settings.py
WAGTAIL_REUSABLE_BLOCKS = {
    # Template for rendering blocks (default: 'wagtail_reusable_blocks/reusable_block.html')
    'TEMPLATE': 'my_app/custom_template.html',

    # Whether to register the default ReusableBlock snippet (default: True)
    'REGISTER_DEFAULT_SNIPPET': True,

    # Maximum nesting depth for nested blocks (default: 5)
    'MAX_NESTING_DEPTH': 5,
}

Available Settings

Setting Default Description
TEMPLATE 'wagtail_reusable_blocks/reusable_block.html' Template used to render blocks
REGISTER_DEFAULT_SNIPPET True Auto-register default ReusableBlock snippet
MAX_NESTING_DEPTH 5 Maximum depth for nested reusable blocks

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:

  1. Create a ReusableBlock with your content
  2. Create another ReusableBlock that references the first one
  3. 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: Block 'A' references block 'B' which creates a cycle.

Cause: You've created a circular reference where Block A → Block B → Block A.

Solution: Remove one of the references to break the cycle. Reusable blocks can be nested, but not in circles.

Maximum Nesting Depth Exceeded

Error: Maximum nesting depth exceeded (displayed as a warning in the rendered output)

Cause: You've nested reusable blocks deeper than the configured MAX_NESTING_DEPTH (default: 5).

Solution:

  • Reduce nesting depth by refactoring your block structure
  • Or increase MAX_NESTING_DEPTH in settings (not recommended beyond 10)

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 (Current)

  • ✅ 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 (Planned)

  • Define placeholders within reusable blocks
  • Inject content into slots from page level
  • Component composition without code changes

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtail_reusable_blocks-0.1.0.tar.gz (21.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wagtail_reusable_blocks-0.1.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file wagtail_reusable_blocks-0.1.0.tar.gz.

File metadata

  • Download URL: wagtail_reusable_blocks-0.1.0.tar.gz
  • Upload date:
  • Size: 21.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for wagtail_reusable_blocks-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bfcde9da7c055b580cd6a9a38ead87aa12dc45a6f5b9114ff00868b943c07ad9
MD5 d307d1607d892e0eca44c3f5e2470b01
BLAKE2b-256 47c7dfa143c72997d8ebf910776cd41b9c78926ee445bc1de7f29d45d177803d

See more details on using hashes here.

Provenance

The following attestation bundles were made for wagtail_reusable_blocks-0.1.0.tar.gz:

Publisher: publish.yml on kkm-horikawa/wagtail-reusable-blocks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wagtail_reusable_blocks-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for wagtail_reusable_blocks-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1c8fc6ab3cae0e740cea3bf5e5206af8c7340ee60457289f40076d7d86a385cc
MD5 63c151561e47d20532b6aba0ac5de5f8
BLAKE2b-256 02737d3bc63ec5b3bfc26742c8dde8ca620d2a2f52bad549c01b59fd81a4e679

See more details on using hashes here.

Provenance

The following attestation bundles were made for wagtail_reusable_blocks-0.1.0-py3-none-any.whl:

Publisher: publish.yml on kkm-horikawa/wagtail-reusable-blocks

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page