Skip to main content

django_slots

Allows multiline strings to be captured and passed to template tags.

Demo

  1. Register a component
# app/templatetags/component_tags.py
from django_slots import Library, Component

register = Library()


@register.block_component
class Details(Component):
  pass
  1. Create a template
{# app/templates/components/details.html #}
<details>
<summary>{{ summary|default:slots.summary }}</summary>
{{ slot }}
</details>

Usage:

{% load component_tags %}
{% load slot_tags %}

{% details summary="the summary" %}
  the default slot
{% /details %}

{% details %}
  {% slot summary %}the <b>summary</b>{% /slot %}
  the default slot
{% /details %}

Installation

pip install django-slots
INSTALLED_APPS = [
    # ...
    
    'django_slots',
]

Slots

Use {% slot name %}{% /slot %} to capture and name a slot. These slots will be available in the template in a dictionary called slots. eg {{ slots.name }}.

Any lines not surrounded by a slot tag will be available in the component template as {{ slot }}.

Template

The default the template path is components/<component_name>.html.

Use template_name attribute or get_template_name() method to override.

Change name

By default, the template tag name will be the Component class name converted to snake case. Use the name attribute to override.

eg:

from django_slots import Component, Library

register = Library()


@register.inline_component
class Button(Component):
    name = 'btn'
{% btn %}

Change name format

By default, inline tags are named "{name}/" and block tags are named "{name}", "/{name}". To change this format specify inline_tag_name and block_tag_names attributes.

eg:

from django_slots import Component, Library

register = Library()


class AppComponent(Component):
    inline_tag_name = "{name}end"
    block_tag_names = "{name}", "end{name}"    

    
@register.component
class Button(AppComponent):
    pass
{% buttonend %}

{% button %}{% endbutton %}

Inline only template tag

Use @register.inline_component to only allow {% inline/ %} use.

Block only template tag

Use @register.block_component to only allow {% block %}{% /block %} use.

Validate arguments

Implement def get_content_data(slots, **kwargs) to validate arguments.

eg:

from django_slots import Component, Library

register = Library()


@register.component
class Button(Component):
    STYLE = ["primary", "secondary"]
    def get_context_data(self, slots, *, style: str):
        if style not in self.STYLE:
            raise self.validation_error(f"style {style!r} not in {self.STYLE!r}")
        return super().get_context_data(slots, style=style)

Namespace components

Components can be namespaced which is useful for creating a third party app.

from django_slots import Library, Component

register = Library()


class NHSUKComponent(Component):
    namespace = 'nhsuk'


@register.component
class Button(NHSUKComponent):
    pass
{% nhsuk:button %}
  Save and continue
{% /nhsuk:button %}

See https://github.com/nwjlyons/nhsuk-components

Metadata

Release files for django-slots 0.2.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-slots 0.2.7
File Size Uploaded Provenance Actions
django_slots-0.2.7.tar.gz 5.3 kB Without provenance Details for django_slots-0.2.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-slots 0.2.7
File Interpreter ABI Platform Actions
django_slots-0.2.7-py3-none-any.whl Python 3 none any Details for django_slots-0.2.7-py3-none-any.whl

Total release size: 10.9 kB

Release files / django_slots-0.2.7.tar.gz

Download URL django_slots-0.2.7.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
098b37917808f8ba744820203b8f3688e588664e522ab7618f63f53989df42b0
BLAKE2b-256 checksum
How to use checksums
3f285779a20f7882d5c3d2310764f7adca794c09a0c7ad52522c92750ceeb0a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.2 Darwin/21.0.1

Release files / django_slots-0.2.7-py3-none-any.whl

Download URL django_slots-0.2.7-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
638676a4e3530e949694ed7335d7a15a54b42c8f4bd6e056e2426f6554efdbc3
BLAKE2b-256 checksum
How to use checksums
fd2e6b48e20791555f9614ce9c6406d006cedc7502bf71f9bd6a8f7180f5f16d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.2 Darwin/21.0.1

Security for django-slots 0.2.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page