Skip to main content

Django Simple Components

PyPI version PyPI Supported Python Versions PyPI Supported Django Versions Coverage)

Django Simple Components is a small package to easily create components inside your templates without saving them in the templates folder.

Quick start

1. Install package:

To get started, install the package from pypi:

pip install django-simple-components

Now you can add simple_components to your django project. Change your INSTALLED_APPS setting like this:

INSTALLED_APPS = [
    ...,
    "simple_components",
]

Optionally, you can specify simple_components as builtins and this will be available in any of your templates without additionally specifying {% load simple_components %}:

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [BASE_DIR / "templates"],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
            "builtins": ["simple_components.templatetags.simple_components"],
        },
    },
]

If you choose not use it as a built-in, you will need to add {% load simple_components %} to the top of your template whenever you want to use simple components.

2. Create component inside your template:

You can define a base component that can be reused later. For example:

{% load simple_components %}

{% #set_component "card" %}
    <div class="card">
        <h3>{{ title }}</h3>
        <p>{{ description }}</p>
    </div>
{% /set_component %}

<div class="card-list">
    {% component "card" title="Post 1" description="..." %}
    {% component "card" title="Post 2" description="..." %}
    {% component "card" title="Post 3" description="..." %}
</div>

You can also use named slots to change the contents of a component as quickly as possible:

{% load simple_components %}

{% #set_component "profile" %}
    <div class="profile">
        <div class="avatar">
            {% @set_slot "avatar" %}
                <!-- fallback username as bdage -->
                <span class="badge">{{ username.0 }}</span>
            {% /set_slot %}
        </div>
        <div class="info">
            {{ username }}
        </div>
    </div>
{% /set_component %}

{% #set_component "card" %}
    <div class="card">
        {% set_slot "image" %}

        <h3>{{ title }}</h3>
        <p>{{ description }}</p>
    </div>
{% /set_component %}

<div class="page">
    {% component "profile" username="Default" %}

    <div class="card-list">
        {% #component "card" title="Post 1" description="..." %}
            {% @slot "image" %}
                <img src="..." alt="slot image" />
            {% /slot %}
        {% /component %}
    </div>
</div>

3. Hooray! Everything is ready to use it.

Template syntax

Create component

To define a component you need to use the Django tag {# set_component name %} where name is the name of the component, listed in quotes. This is a paired tag, which means it needs to be closed:

{% #set_component "card" %}
    <p>{{ title }}</p>
{% /set_component %}

Use component

In the example above, we defined the "card" component and specified a variable that will be used during rendering later. Now, to use this component, you just need to write:

{% component "card" title="Post" %}

Components can also render children - anything specified between the opening and closing component tags:

{% #set_component "card" %}
    <div class="c1 c2 c3 c4 c5">
        <p>{{ title }}</p>
        {{ children }}
    </div>
{% /set_component %}

{% #component "card" title="Post" %}
    <p>Simple</p>
    <p>Content</p>
{% /component %}

Create slots

Slots are a powerful opportunity to upgrade your components! Slots can accept any content. Each slot has a name, and is specified inside the set_component tag:

{% #set_component "card" %}
    <div class="card">
        <div class="card-header">
            <h3>{{ title }}</h3>
        </div>
        <div class="card-body">
            {% set_slot "body" %}
        </div>
    </div>
{% /set_component %}

Any slot can have a fallback template, which will be displayed if this slot was not passed to the component. Otherwise, a SlotNotFoundError will be raised:

{% @set_slot "custom" %}
    fallback
{% /set_slot %}

Use slots

To use slots in components, you need to pass the slot when rendering where it is needed:

{% #set_component "card" %}
    <div class="card">
        ...
        <div class="card-body">
            {% set_slot "body" %}
        </div>
    </div>
{% /set_component %}

{% #component "card" %}
    {% @slot "body" %}
        render slot inside card-body class
    {% /slot %}
{% /component %}

Summarize

All paired tags in simple components close with %{ /tag %}, where tag can be set_component, set_slot, component.

There are {% component %} and {% set_slot %} which can be defined on the same line. For the component, this means that it cannot use slots and children. For set_slot this means that the fallback template is not specified:

{% component "card" title="Post" description="..." %}
{% set_slot "avatar" %}

To define a component or use it, you can specify # at the beginning of the tag (only for closable tags):

{% #set_component name="simple" %}
    {{ children }}
{% /set_component %}
{% #component name="simple" %}
    component with children content
{% /component %}

To define a slot or use it, you can specify @ at the beginning of the tag (only for closable tags):

{% @set_slot "custom" %}
    fallback
{% /set_slot %}
{% @slot "custom" %}
    render slot
{% /slot %}

Contributing

If you would like to suggest a new feature, you can create an issue on the GitHub repository for this project. Also you can fork the repository and submit a pull request with your changes.

License

This project is licensed under the MIT License. See the LICENSE file for more information.

Metadata

Release files for django-simple-components 0.1.0

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-simple-components 0.1.0
File Size Uploaded Provenance
django-simple-components-0.1.0.tar.gz 9.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-simple-components 0.1.0
File Interpreter ABI Platform
django_simple_components-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.4 kB

Release files / django-simple-components-0.1.0.tar.gz

Download URL django-simple-components-0.1.0.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d101e4de238f3aa6ecdb97d164151b7d75dee345056152d12d64b4f5c4484337
BLAKE2b-256 checksum
How to use checksums
df04697fb8d99e3bfd4681a71655622c39d6326786d97ed5763c3ec8b2d7642d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release files / django_simple_components-0.1.0-py3-none-any.whl

Download URL django_simple_components-0.1.0-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d311dbf3d781c04cb32c57f9b54f461b199647bb34128b280fb8a4a7551c17f
BLAKE2b-256 checksum
How to use checksums
992b10f783bd57f6827b892150e7f0eded18888463264be01e414d9a62cdf622
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Security for django-simple-components 0.1.0

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