Skip to main content

Django includecontents component-like tag

Project description

Django IncludeContents

PyPI version Django Support

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

📚 Full Documentation

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_includecontents-4.0.1.tar.gz (131.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_includecontents-4.0.1-py3-none-any.whl (64.4 kB view details)

Uploaded Python 3

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

Hashes for django_includecontents-4.0.1.tar.gz
Algorithm Hash digest
SHA256 adbe0c3a9385ac0743433e1a2358822a658fe72c19b56c215eec46a23d7e1aea
MD5 668a1507195bce5440ca3fdcf2cbe2ee
BLAKE2b-256 04d075a028844ded33bb2f765878d2c50e7430bd2112cf8042e94b7b210f8f14

See more details on using hashes here.

File details

Details for the file django_includecontents-4.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_includecontents-4.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e8df11c79876394520aa8a9e6a564f7782b726054ea5ef78c243337ad0fbcde1
MD5 05eb899911014711feb348416b272c77
BLAKE2b-256 5fee5f94aaeedf61ab7da2446f9659c38828be088860fe9968f5aefb6308cce2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page