django-components-preprocessor
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_preprocessorfunction and use it to wrap theloadersoption in yourTEMPLATESsetting:
# 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.
Metadata
Release files for django-components-preprocessor 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_components_preprocessor-0.3.0.tar.gz | 7.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_components_preprocessor-0.3.0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 12.2 kB
Release files / django_components_preprocessor-0.3.0.tar.gz
| Download URL | django_components_preprocessor-0.3.0.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
26472d0c10e4b5654d96ec4cecee87fa1adeefc1c09d4586c53aac70d4fe2f07
|
|
BLAKE2b-256 checksum How to use checksums |
8e90b663db8e14e30578da6f707e1f86fb03d81bd90dc430ad0edee3d935e714
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.31.0
|
Release files / django_components_preprocessor-0.3.0-py2.py3-none-any.whl
| Download URL | django_components_preprocessor-0.3.0-py2.py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
c0e5633b97fd0415176dd35601b1b70d1bfc8b8a570dd35e08742e0e08352d0e
|
|
BLAKE2b-256 checksum How to use checksums |
4e33370222a1b78bf2f75315187a34e54cad4f8b515d646bbfe7125d796abbfd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.31.0
|