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.backend.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.
Some HTML formatters (like prettier) insist on quoting HTML attribute values, you can avoid this by optionally wrapping template values in {}
:
<include:card title={mytitle}></include:card>
Component Props
You can define the required or default props of the component in a comment at the top of its template that begins with props
(or def
to match what JinjaX uses). An exception will be raised if a required prop is not provided.
Any other attributes passed to the component that are not listed in this definition will be added to an attrs
context variable that can render them as HTML attributes.
{# props #}
<div {{ attrs }}>
{{ contents }}
</div>
You can also provide default values for these attributes via the {% attrs %}
template tag.
{# props title, large=False #}
<div {% attrs class="card" %}>
...
This example component above 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 a class attribute.
If you want to provide multiple groups of undefined attributes, you can use group.name
as the format.
Then render them with {{ attrs.group }}
(or {% attrs.group %}
if you want fallback values).
For example to call a component like this:
<include:field label="Name" name="first_name" value="John" input.class="wide"></include:field>
It could be defined like this:
{# props value, label="" #}
<div {% attrs class="field" %}>
{% if label %}{{ '<label>'|safe }}{% endif %}
{{ label }}
<input {% attrs.input type="text" value=value %}>
{% if label %}{{ '</label>'|safe }}{% endif %}
</div>
Extended / conditional classes
Prepend your class list with "& "
to have it extended rather than replaced:
{% attrs class="lg" %} {# sets default class attribute to "lg" but can be overridden #}
{% attrs class="& lg p-3" %} {# always add 'lg p-3' classes #}
You can provide conditional classes for the class
attribute using the svelte class directive format:
{# props large=False #}
{% attrs class:lg=large %} {# adds 'lg' class if large prop is truthy #}
{% attrs class:lg %} {# always adds 'lg' class #}
You can use this same conditional format on the component attributes directly:
<include:card title="Hello" class:lg={is_large}>
<p>World</p>
</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.6.tar.gz
.
File metadata
- Download URL: django_includecontents-0.6.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.15.0 CPython/3.11.8 Linux/6.1.85-1-MANJARO
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d93ddb88d4f7147e98db4e28d28521ed1e8d139d7f326c1e209b85a3be0a768 |
|
MD5 | 44d539ee40f67c01e944bb657d2ebca7 |
|
BLAKE2b-256 | ff4527ad7f49ac376c57d4b1a3eae57fc79739db12ab2cf93ff53e45f82c1e8d |
File details
Details for the file django_includecontents-0.6-py3-none-any.whl
.
File metadata
- Download URL: django_includecontents-0.6-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.15.0 CPython/3.11.8 Linux/6.1.85-1-MANJARO
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bd3b363bc34d58e8236d18bdd30be6fb8a64a3d0cd7ebf914fb067281d04ba0 |
|
MD5 | 2583ec9d234d57a504156ae9c337778b |
|
BLAKE2b-256 | 08fd9bba0a0efc173a225b018d8efa744c03150e6d4e9f89b1308fbc8a493189 |