Composition for django templates.
Project description
Django Compose Tag
Compose templates easily
Django Compose tag provide a template tag for templates composition,
with an api close to the include
template tag.
Overview
Write your template as you would for the include tags:
<!-- card.html -->
<article class="card">
<header class="card-header">{{ header }}</header>
<div class="card-body">{{ children }}</div>
<footer class="card-footer">{{ footer }}</footer>
</article>
The children
tag
children
has a special meaning, it will be replaced by the content that's between the {% compose %}
and {% endcompose %}
tags.
{% load compose %}
{% compose "card.html" header="My header" footer="My footer" %}
<p>In {% now "Y" %} we can even put template tags and {{ variables }} in here.</p>
{% endcompose %}
Similarities to include
card.html is a regular template, so we can use it with a regular include when parameters are simple
{% include "card.html" with header="My header" children="My body" footer="My footer" %}
Yet there is small differences between compose
and include
:
- Like
include
,compose
accept variables as parameters:{% compose "card.html" header=var_from_context %}...{% endcompose %}
- Like
include
,compose
accept the same kind of parameters for the template, that include variables:{% compose variable_template %}...{% endcompose %}
- Unlike
include
,compose
doesn't prefix its parameters withwith
- Unlike
include
,compose
doesn't have anonly
parameter. The parent context is never accessible within the composed template.
Requirements
- Python (3.6, 3.7, 3.8, 3.9, 3.10, 3.11)
- Django (2.2, 3.0, 3.1, 3.2, 4.0, 4.1)
We highly recommend and only officially support the latest patch release of each Python and Django series.
Installation
Install using pip
...
pip install django-compose-tags
Add 'django_compose_tagss'
to your INSTALLED_APPS
setting.
INSTALLED_APPS = [
...
'django_compose_tagss',
]
If you use compose a lot we recommend you add it as to your builtins:
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"OPTIONS": {
# ...
"builtins": ["django_compose_tagss.templatetags.compose"],
}
}
]
Alternatives
Django Compose Tag purposely provide a simple api based solely on templates.
If Django Compose Tag doesn't cover your requirements we recommend you take a look at jinja.
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
Hashes for django-compose-tags-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a668b2ea27b774fec2f1e5e86747ca655b7101a6547167722f3a310ff6391ef8 |
|
MD5 | 93f60fc33ff43960b1ecc47fde82ae5a |
|
BLAKE2b-256 | b1c087f703378108828f9281eeef341b213aeb1f6996b253a8ac9e4649313b5c |
Hashes for django_compose_tags-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d73f526e3c8ad43464453562c7b0153a2ee5478326a80c97e0f5b24ad80104ce |
|
MD5 | e4ed62fec9f6928660cebb42923a4b22 |
|
BLAKE2b-256 | 99c12d9174e6f3075162b5c2c4dfa64a178051f4748c1d19ce471431bf10e628 |