Skip to main content

Use Wagtail blocks as reusable components in Django templates

Project description

Wagtail Block Components

Use Wagtail blocks as reusable components in Django templates.

Installation

pip install wagtail-block-components

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'wagtail_block_components',
    # ...
]

Quick Start

1. Define Your Blocks

# myapp/blocks.py
import uuid
from wagtail import blocks
from wagtail.images.blocks import ImageChooserBlock
from wagtail.documents.blocks import DocumentChooserBlock

class AccordionItemBlock(blocks.StructBlock):
    title = blocks.CharBlock(required=True)
    content = blocks.StreamBlock([
        ('paragraph', blocks.RichTextBlock()),
        ('page', blocks.PageChooserBlock()),
        ('image', ImageChooserBlock()),
        ('document', DocumentChooserBlock()),
        ('inline_structblock', blocks.StructBlock([
            ('heading', blocks.CharBlock()),
            ('content', blocks.RichTextBlock()),
        ])),
    ], required=False)

    class Meta:
        template = 'components/accordion_item.html'

class AccordionBlock(blocks.StructBlock):
    items = blocks.ListBlock(AccordionItemBlock())

    def get_context(self, value, parent_context=None):
        context = super().get_context(value, parent_context)
        # Generate unique accordion ID for children to use
        context['accordion_id'] = f"accordion-{uuid.uuid4().hex[:8]}"
        return context

    class Meta:
        template = 'components/accordion.html'

2. Register Components

# myapp/wagtail_hooks.py
from wagtail import hooks
from . import blocks

@hooks.register('register_components')
def register_components():
    return [
        blocks.AccordionBlock,
        blocks.AccordionItemBlock,
    ]

3. Use in Templates

{% load wagtail_block_components %}

{# Self-closing block - empty accordion #}
{% wagtail_block "AccordionBlock" / %}


{# Block with kwargs passed directly #}
{% wagtail_block "AccordionItemBlock" title="Empty Item" / %}


{# Full accordion with all syntax variations #}
{% wagtail_block "AccordionBlock" %}

    {# First item: using block_field for title (template content) #}
    {% block_field "items" %}
        {% wagtail_block "AccordionItemBlock" %}
            {% block_field "title" %}Question 1{% endblock_field %}
            {% block_field "content" %}
                {% block_field "paragraph" %}<p>Answer 1 part 1</p>{% endblock_field %}
                {% block_field "paragraph" %}<p>Answer 1 part 2</p>{% endblock_field %}
            {% endblock_field %}
        {% endwagtail_block %}
    {% endblock_field %}

    {# Second item: using kwargs for title #}
    {% block_field "items" %}
        {% wagtail_block "AccordionItemBlock" title="Question 2" %}
            {% block_field "content" %}
                {% block_field "paragraph" %}<p>Answer 2</p>{% endblock_field %}
            {% endblock_field %}
        {% endwagtail_block %}
    {% endblock_field %}

    {# Third item: using shorthand block_field with variable #}
    {% block_field "items" %}
        {% wagtail_block "AccordionItemBlock" %}
            {% block_field "title" title_var / %}
            {% block_field "content" %}
                {% block_field "inline_structblock" %}
                    {% block_field "heading" %}Inline Struct Heading{% endblock_field %}
                    {% block_field "content" %}<p>Inline Struct Content</p>{% endblock_field %}
                {% endblock_field %}
            {% endblock_field %}
        {% endwagtail_block %}
    {% endblock_field %}

    {# Fourth item: shorthand with string literal + ChooserBlocks #}
    {% block_field "items" %}
        {% wagtail_block "AccordionItemBlock" %}
            {% block_field "title" "Question 4" / %}
            {% block_field "content" %}
                {% block_field "image" test_image / %}
                {% block_field "document" test_document / %}
                {% block_field "page" test_page / %}
                {% block_field "paragraph" %}<p>ChooserBlocks work!</p>{% endblock_field %}
            {% endblock_field %}
        {% endwagtail_block %}
    {% endblock_field %}

{% endwagtail_block %}

How It Works

The tag processes your template syntax into the raw value format Wagtail blocks expect, then calls block.to_python() and block.render().

Syntax Options

Passing Field Values

As kwargs when the value is simple:

{% wagtail_block "AccordionItemBlock" title="Question 1" %}
    {# ... #}
{% endwagtail_block %}

As block_field with content when you need template rendering:

{% wagtail_block "AccordionItemBlock" %}
    {% block_field "title" %}Question {{ question_number }}{% endblock_field %}
    {# ... #}
{% endwagtail_block %}

As block_field shorthand when passing simple values or variables:

{% wagtail_block "AccordionItemBlock" %}
    {% block_field "title" "My Title" / %}
    {# Or from context: #}
    {% block_field "title" my_title_var / %}
{% endwagtail_block %}

Block Types

StructBlock - Use {% block_field %} for each field:

{% wagtail_block "AccordionItemBlock" title="Question" %}
    {% block_field "content" %}
        {% block_field "paragraph" %}<p>Answer</p>{% endblock_field %}
    {% endblock_field %}
{% endwagtail_block %}

ListBlock - Repeat {% block_field %} with the same name:

{% wagtail_block "AccordionBlock" %}
    {% block_field "items" %}
        {% wagtail_block "AccordionItemBlock" title="Q1" %}...{% endwagtail_block %}
    {% endblock_field %}
    {% block_field "items" %}
        {% wagtail_block "AccordionItemBlock" title="Q2" %}...{% endwagtail_block %}
    {% endblock_field %}
{% endwagtail_block %}

StreamBlock - Use nested {% block_field %} for typed content:

{% block_field "content" %}
    {% block_field "paragraph" %}<p>First paragraph</p>{% endblock_field %}
    {% block_field "paragraph" %}<p>Second paragraph</p>{% endblock_field %}
    {% block_field "image" my_image / %}
    {% block_field "page" my_page / %}
    {% block_field "document" my_document / %}
    {% block_field "inline_structblock" %}
        {% block_field "heading" "Nested Heading" / %}
        {% block_field "content" %}<p>Nested content</p>{% endblock_field %}
    {% endblock_field %}
{% endblock_field %}

ChooserBlock fields (PageChooserBlock, ImageChooserBlock, DocumentChooserBlock) - Pass pk or model instance:

{# Using pk #}
{% block_field "page" 123 / %}

{# Using model instance (pk extracted automatically) #}
{% block_field "page" my_page / %}

Use in Both Places

The same blocks work in Wagtail StreamFields:

from wagtail.models import Page
from wagtail.fields import StreamField
from myapp.blocks import AccordionBlock

class HomePage(Page):
    body = StreamField([
        ('accordion', AccordionBlock()),
    ])

And in regular Django views:

{% load wagtail_block_components %}

{% wagtail_block "AccordionBlock" %}
    {# ... #}
{% endwagtail_block %}

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_block_components-0.1.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

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

wagtail_block_components-0.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for wagtail_block_components-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4baf971d6659eab1bf6ec4887122cfa7bdeb837b251a908f06ac88ac0045b4c1
MD5 9727dbd3ffda1497855334f35d64967b
BLAKE2b-256 fdc7ee79ceaf05a83d5e910407e06b88e904ddc4e624efec455653c2cb739dc8

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on joeyjurjens/wagtail-block-components

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_block_components-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for wagtail_block_components-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 04a61e555bbda1e3afbdcf548da17eded598bce514bd059a4e9556dc1d248e26
MD5 b6b2789e44bb5b11060ca063e6ccdbfd
BLAKE2b-256 402c7227794ac471a088644f581e2e4ba30a384d9b4ef44c14bca96d4d74a9b4

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on joeyjurjens/wagtail-block-components

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