Django includecontents component-like tag
Project description
Django IncludeContents
Component-like Django template tags with HTML syntax support. Full feature parity between Django templates and Jinja2.
Features
- 🧩 Component Templates: Create reusable template components with isolated contexts
- 📝 HTML Syntax: Use familiar HTML-like syntax for components (
<include:my-card>) - 🎯 Props System: Define required and optional props with validation
- 🎨 Advanced Styling: Conditional classes, extended classes, and CSS utilities
- 🔀 Conditional Wrapping: Clean conditional HTML wrapper syntax with
{% wrapif %} - 🎭 Icon System: SVG sprite generation from Iconify icons and local SVG files (
<icon:home>)
Quick Start
Installation
pip install django-includecontents
Setup
Choose your template engine:
=== "Django Templates"
Replace your Django template backend in `settings.py`:
```python
TEMPLATES = [
{
'BACKEND': 'includecontents.django.DjangoTemplates',
# ... rest of your template config
},
]
```
=== "Jinja2"
Add the Jinja2 extension in `settings.py`:
```python
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'OPTIONS': {
'extensions': [
'includecontents.jinja2.IncludeContentsExtension',
],
},
},
]
```
See [Jinja2 Setup](https://smileychris.github.io/django-includecontents/getting-started/jinja2-setup/) for complete instructions.
Create a Component
templates/components/welcome-card.html
{# props title, subtitle="" #}
<div class="card">
<h2>{{ title }}</h2>
{% if subtitle %}<p>{{ subtitle }}</p>{% endif %}
<div class="content">{{ contents }}</div>
</div>
Use the Component
<include:welcome-card title="Hello World" subtitle="Getting started">
<p>Your component content goes here!</p>
</include:welcome-card>
Result
<div class="card">
<h2>Hello World</h2>
<p>Getting started</p>
<div class="content">
<p>Your component content goes here!</p>
</div>
</div>
Template Tag Syntax
If you prefer traditional Django template syntax:
{% load includecontents %}
{% includecontents "components/welcome-card.html" title="Hello World" subtitle="Getting started" %}
<p>Your component content goes here!</p>
{% endincludecontents %}
Documentation
- Getting Started - Installation and setup
- Jinja2 Setup - Jinja2 template engine setup
- Quick Start Guide - Get started in 5 minutes
- HTML Components - Modern component syntax
- Best Practices - Building great components
Examples
Named Content Blocks
<include:article title="My Article">
<content:header>
<h1>Article Title</h1>
<p>By {{ author }}</p>
</content:header>
<p>Main article content...</p>
<content:sidebar>
<h3>Related Links</h3>
</content:sidebar>
</include:article>
Conditional Wrapping
{% load includecontents %}
{% wrapif user.is_authenticated %}
<a href="/profile" class="user-link">
{% contents %}Welcome, {{ user.name }}{% endcontents %}
</a>
{% endwrapif %}
Modern JavaScript Framework Integration
<!-- Vue.js and Alpine.js attributes work seamlessly -->
<include:button @click="handleClick()" :disabled="isLoading">
Submit
</include:button>
<include:modal x-on:click="open = false" x-show="open">
Modal content
</include:modal>
<!-- Nested attributes for complex components -->
<include:form inner.class="form-control" button.@click="submit()">
Form content
</include:form>
Dynamic Components
<include:button variant="primary" {disabled} class:loading="{{ is_processing }}">
{% if is_processing %}Processing...{% else %}Submit{% endif %}
</include:button>
Icons
# settings.py
STATICFILES_FINDERS = [
'includecontents.icons.finders.IconSpriteFinder', # Must be first for icons
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
INCLUDECONTENTS_ICONS = {
'icons': [
'mdi:home', # Use as <icon:home>
'tabler:user', # Use as <icon:user>
'icons/logo.svg' # Use as <icon:logo>
]
}
Note: Icon names auto-generate from config:
'mdi:home'→<icon:home>,'icons/logo.svg'→<icon:logo>
<icon:home class="w-6 h-6" />
<icon:user class="avatar" use.role="img" />
<icon:logo class="brand" />
Requirements
- Python: 3.8+
- Django: 3.2+
License
MIT License. See LICENSE for details.
Contributing
Contributions welcome! Please see our GitHub Issues for bug reports and feature requests.
Support
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_includecontents-4.0.1.tar.gz.
File metadata
- Download URL: django_includecontents-4.0.1.tar.gz
- Upload date:
- Size: 131.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.25.9 CPython/3.12.3 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
adbe0c3a9385ac0743433e1a2358822a658fe72c19b56c215eec46a23d7e1aea
|
|
| MD5 |
668a1507195bce5440ca3fdcf2cbe2ee
|
|
| BLAKE2b-256 |
04d075a028844ded33bb2f765878d2c50e7430bd2112cf8042e94b7b210f8f14
|
File details
Details for the file django_includecontents-4.0.1-py3-none-any.whl.
File metadata
- Download URL: django_includecontents-4.0.1-py3-none-any.whl
- Upload date:
- Size: 64.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.25.9 CPython/3.12.3 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e8df11c79876394520aa8a9e6a564f7782b726054ea5ef78c243337ad0fbcde1
|
|
| MD5 |
05eb899911014711feb348416b272c77
|
|
| BLAKE2b-256 |
5fee5f94aaeedf61ab7da2446f9659c38828be088860fe9968f5aefb6308cce2
|