Skip to main content

A preprocessor for Django templates that replaces custom HTML tags with component tags.

Project description

django-components-preprocessor

License

Description

This project allows you to use regular HTML tags for your django-component components.

Before

{% component_block "calendar" attr="value" %}
  {% fill "header" %}
    Custom header
  {% endfill %}
{% encomponent_block %}

After

<calendar attr="value">
  <calendar.header>
    Custom header
  </calendar.header>
</calendar>

Installation

  • Install the package using pip:
pip install django-components-preprocessor
  • Import the with_components_preprocessor function and use it to wrap the loaders option in your TEMPLATES setting:
# settings.py
from django_components_preprocessor import with_components_preprocessor
...

# Example TEMPLATES setting
TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "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",
            ],
            "loaders": with_components_preprocessor(
                "django.template.loaders.filesystem.Loader",
                "django.template.loaders.app_directories.Loader",
                "django_components.template_loader.Loader",
            ),
            "builtins": [
                "django_components.templatetags.component_tags",
            ],
        },
    },
]

How it works

Your templates will be preprocessed before being passed to the regular django template engine. The preprocessor will replace custom tags (i.e. <calendar />) with the regular django-component tags (i.e. {% component "calendar" }). Additionally, custom tags with dots (i.e. <calendar.header />) will be replaced with the regular django-component tags (i.e. {% fill "header" %}), where the slot name is the last part of the tag name split at the dot. The tags are matched by name and are case-sensitive.

Currently, custom tags are found using regular expressions, which is obviously a bit whacky. It does however work for most cases, and is a lot more readable than the regular django-component tags. A more robust solution would be to use a proper HTML parser, but that's a bit overkill at this point in time.

If, due to the limitations of the regular expressions, you find that the preprocessor doesn't work for your use case, you can always use the regular django-component tags as you normally would.

License

This project is licensed under the MIT License.

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_components_preprocessor-0.3.0.tar.gz (7.9 kB view hashes)

Uploaded Source

Built Distribution

django_components_preprocessor-0.3.0-py2.py3-none-any.whl (4.3 kB view hashes)

Uploaded Python 2 Python 3

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