Django includecontents component-like tag
Project description
Django IncludeContents tag
Provides a component-like {% includecontents %}
tag to Django.
For example:
{% load includecontents %}
{% includecontents "hello.html" %}
<p>World</p>
{% endincludecontents %}
It also provides a simple Django template engine that extends this tag to work like an HTML component.
In this example, it will include and render components/card.html
:
<include:card title="Hello">
<p>World</p>
</include:card>
This engine also allows for multi-line template tags. For example:
{% if
user.is_authenticated
and user.is_staff
%}
...
{% endif %}
Installation
pip install django-includecontents
To use the custom template engine, replace the default DjangoTemplates
backend in your settings:
TEMPLATES = [
{
'BACKEND': 'includecontents.backends.Templates',
...
},
]
This engine also adds includecontents
to the built-in tags so there is no need to load it.
If you don't want the custom engine, just add this app to your INSTALLED_APPS
and load the tag in your templates:
INSTALLED_APPS = [
...
'includecontents',
]
{% load includecontents %}
...
{% includecontents %}...{% endincludecontents %}
Template tag usage
The includecontents
tag works like the include
tag but the contents is rendered and passed to the included template as a contents
variable.
{% includecontents "hello.html" %}
<p>World</p>
{% endincludecontents %}
Named contents blocks
You can also have named contents blocks within the component content.
For example:
{% includecontents "hello.html" %}
<p>World</p>
{% contents footer %}Footer{% endcontents %}
{% endincludecontents %}
Where hello.html
template could look something like:
<div class="card">
<div class="content">
{{ contents }}
</div>
{% if contents.footer %}
<div class="footer">
{{ contents.footer }}
</div>
{% endif %}
</div>
HTML Components Usage
Create a components
directory in your templates directory. This is where you will put your component templates that are used via the HTML component format.
These components are normal Django templates that will be rendered with an isolated context. The context is passed to the component via component's attributes.
Components must be CamelCase and not match any standard HTML tags.
For example, a components/card.html
template could look like:
<div class="card">
<h2>{{ title }}</h2>
<div class="content">
{{ contents }}
</div>
</div>
Which will allow you to use it like this (without the need to load any template library):
<include:card title="Hello">
<p>World</p>
</include:card>
You can use named {% contents %}
blocks, just like with the includecontents
tag.
Attrs
You can define which attributes should be passed to the component in a comment at the top of the component template, and others that can have a default value.
Any other attributes passed to the component will be added to an attrs
context variable that can render them as HTML attributes.
You can also provide default values for these attributes via the default_attrs
filter.
{# def title, large=False #}
<div {{ attrs|default_attrs:'class="card"' }}>
This would require a title
attribute and allow an optional large
attribute. Any other attributes will be rendered on the div, with a default class of card
if you don't specify any other class.
So the following tags would all be valid:
<include:card title="Hello"></include:card>
<include:card title="Hello" large></include:card>
<include:card title="Hello" id="topcard" class="my-card"></include:card>
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 django_includecontents-0.3.tar.gz
.
File metadata
- Download URL: django_includecontents-0.3.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.12.4 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c9c430ec9bb27c9f8cebc8aed831ad4b9fc4093194fd782bfd3aed360cd3187 |
|
MD5 | b2a29f876f422440a9a3ec25a1756086 |
|
BLAKE2b-256 | 1730affb649e79d9f83f03bac7693deba0fd9b93a32fb5083c981f4519a2a617 |
File details
Details for the file django_includecontents-0.3-py3-none-any.whl
.
File metadata
- Download URL: django_includecontents-0.3-py3-none-any.whl
- Upload date:
- Size: 2.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.12.4 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ae39b543f8cd1e85e040bb375ccc055787e4faa2319d998f83590d532503884 |
|
MD5 | 2ef92a343ead13bf530d7f0f069af8e9 |
|
BLAKE2b-256 | bfb677697f32cdf05d8830c432b393aa743b2cdb4c1b2b5148f8f2b7b23926d3 |