Skip to main content

django_slots = inclusion tag + blocks

Project description

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 thh component template as {{ slot }}.

Custom 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 tag name

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

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

django_slots-0.2.6.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

django_slots-0.2.6-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file django_slots-0.2.6.tar.gz.

File metadata

  • Download URL: django_slots-0.2.6.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Darwin/21.0.1

File hashes

Hashes for django_slots-0.2.6.tar.gz
Algorithm Hash digest
SHA256 21c27f35767fec06f0b50752eac25cd66c6faae7ee8736d0c4ebb38bec009dee
MD5 dad7cbeefa58461c3a91785d36e5faf5
BLAKE2b-256 0b77e77f8ce987429c7073034300a4fff8bd13eb87dcaa536eb719405ab7e160

See more details on using hashes here.

File details

Details for the file django_slots-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: django_slots-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Darwin/21.0.1

File hashes

Hashes for django_slots-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 07c9ff146a8517f739fa7cc98f53990258f407a6f6e990d0e96d3b4fcdb77b7a
MD5 de5345d96b96df15edef52588343248c
BLAKE2b-256 4c7bfd4a2d6fdf6c87a5679525ddaefb4446fa4e3b04f660b23a391df2ffd2cc

See more details on using hashes here.

Supported by

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