Adds more bracket angles to Django templates </>
Project description
dj-angles </>
Add some more bracket angles to your Django templates
This code is experimental -- use with caution! Or maybe not at all! 🤷
📦 Package located at https://pypi.org/project/dj-angles/.
⭐ Features
- Use HTML-like elements in Django templates, e.g.
<dj-partial />
instead of{% include 'partial.html' %}
- Can be sprinkled in as needed, but does not remove existing Django functionality
- Pretend like you are writing React components, but without dealing with JavaScript at all
- Lets you excitedly tell your friends about how neat the Shadow DOM is
⚡ Installation
- Create a new Django project or
cd
to an existing project pip install dj-angles
to install thedj-angles
packageEdit settings.py
TEMPLATES
and add"dj_angles.template_loader.Loader",
as the first loader. Note: you might need to add the"loaders"
key since it is not there by default: https://docs.djangoproject.com/en/stable/ref/templates/api/#django.template.loaders.cached.Loader.
TEMPLATES = [
"BACKEND": "django.template.backends.django.DjangoTemplates",
# "APP_DIRS": True, # this cannot be specified if loaders are explicitly set
"DIRS": [],
"OPTIONS": {
"builtins": builtins,
"context_processors": [
"django.template.context_processors.request",
"django.template.context_processors.debug",
"django.template.context_processors.static",
],
"loaders": [
(
"django.template.loaders.cached.Loader",
[
"dj_angles.template_loader.Loader", # this is required for `dj-angles`
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
],
)
],
},
]
✨ Inspiration
I have been interested in Django components and encapsulating functionality for a long time (see django-unicorn, dlitejs, etc), but had never thought of using HTML directly until I looked at Cotton by wrabit.
💡
Since <c-component />
was a high-powered wrapper around {% include %}
, what if other Django templatetags could also be wrapped? This library is an experiment to see what that experience is like and how well it works.
💥 Template example
base.html
<dj-block 'content'>
</dj-block 'content'>
index.html
<dj-extends 'base.html' /> <!-- {% extends 'base.html' %} -->
<dj-block 'content'> <!-- {% block 'content' %} -->
<dj-include 'partial.html' /> <!-- {% include 'partial.html' %} -->
<dj-verbatim> <!-- {% verbatim %} -->
This is verbatim: {% include %}
</dj-verbatim> <!-- {% endverbatim %} -->
<dj-comment> <!-- {% comment %} -->
this is a comment
</dj-comment> <!-- {% endcomment %} -->
<dj-#>this is another comment</dj-#> <!-- {# this is another comment #} -->
<dj-autoescape-on> <!-- {% autoescape-on %} -->
This is escaped
</dj-autoescape-on> <!-- {% endautoescape %} -->
<dj-autoescape-off> <!-- {% autoescape off %} -->
This is not escaped
</dj-autoescape-off> <!-- {% endautoescape %} -->
<dj-csrf /> <!-- {% csrf_token %} -->
<dj-debug /> <!-- {% debug %} -->
</dj-block 'content'> <!-- {% endblock 'content' %} -->
partial.html
<div style="border: 1px solid red;">
<p>
This is a partial: {{ now|date:"c" }}
</p>
</div>
<style>
p {
color: green;
}
</style>
🪄 Include tags
These are all equivalent ways to include partial HTML files.
<dj-include 'partial.html' />
<dj-partial />
<$partial />
They all compile to the following Django template syntax.
{% include 'partial.html' %}
⤵️ Directories
Accessing templates in directories is supported even though technically forward-slashes aren't permitted in a custom element. It will definitely confound most HTML syntax highlighters.
<dj-include 'directory/partial.html' />
<dj-directory/partial />
<$directory/partial />
🥷 CSS scoping
To encapsulate component styles, enable the Shadow DOM for the partial. This will ensure that any style
element in the partial will be contained to that partial. The downside is that the Shadow DOM does not allow outside styles in, other than CSS variables.
<p>These are all equivalent ways to include partials.</p>
<dj-include 'partial.html' shadow />
<dj-partial shadow />
<dj-partial! />
<$partial! />
They all compile to the following Django template syntax.
<template shadowrootmode='open'>{% include 'partial.html' %}</template>
- More details about declaratively creating shadow root: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template#shadowrootmode
- More details about using the Shadow DOM: https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM
- Shadow DOM styling: https://javascript.info/shadow-dom-style
🛠️ Other tags
#
<dj-#>...</dj-#>
{# ... #}
autoescape-off
<dj-autoescape-off>
...
</dj-autoescape-off>
{% autoescape off %}
{% endautoescape %}
autoescape-on
<dj-autoescape-on>
...
</dj-autoescape-on>
{% autoescape on %}
{% endautoescape %}
block
<dj-block 'content'>
...
</dj-block 'content'>
{% block 'content' %}
...
{% endblock 'content' %}
csrf
, csrf-token
, csrf-input
<dj-csrf />
{% csrf_token %}
comment
<dj-comment>
...
</dj-comment>
{% comment %}
...
{% endcomment %}
debug
<dj-debug />
{% debug %}
extends
<dj-extends 'base.html' />
{% extends 'base.html' %}
filter
<dj-filter ... />
{% filter ... %}
lorem
<dj-lorem />
{% lorem %}
now
<dj-now />
{% now %}
spaceless
<dj-spaceless>
...
</dj-spaceless>
{% spaceless %}
...
{% endspaceless %}
templatetag
<dj-templatetag ... />
{% templatetag ... %}
verbatim
<dj-verbatim>
...
</dj-verbatim>
{% verbatim %}
...
{% endverbatim %}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dj_angles-0.1.1.tar.gz
.
File metadata
- Download URL: dj_angles-0.1.1.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdde09bafd674e6497fe57cdb9c8516294eed02b41d781abae2bb2791df3b645 |
|
MD5 | ea2e0f78cad2a6ffca04b2d4d1a8068a |
|
BLAKE2b-256 | 8bd33ad757cef24fed35101b07ba431753434a8777e3e2dde98cf6c76f2e172f |
File details
Details for the file dj_angles-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: dj_angles-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28e2512965128bbea16c3ceb4ee17e4186a49783e7339e5ada1eb20b3ac4615e |
|
MD5 | 0d44a8d327ef3ac928aa57835dc3f82d |
|
BLAKE2b-256 | 96c741a3b20bf14669d7a82766fc1033bd9fb4700381697cae32c12c40700b3d |