A django template tag that simplifies nested templates.
Project description
Django Sandwich Tags
A django template tag that simplifies nested templates.
Version: 0.2.1
Overview
The sandwich template tag is a Django template tag that allows for easy composition of templates by wrapping a child template inside a parent template. This is particularly useful when building reusable UI components where a consistent layout needs to be enforced while allowing for dynamic content injection. It complements Django's built-in template inheritance by enabling support for nestable, reusable templates and components, rather than replacing it.
Installation
To use the sandwich template tag in your Django project, install the package and add it to your Django app's template tag modules.
-
Install the
sandwichpackage from PyPI$ pip install django-sandwich-tag
-
Add
'sandwich_tag'toINSTALLED_APPS:INSTALLED_APPS = [ ..., "sandwich_tag", ..., ]
-
Load the template tag in your Django template:
{% load sandwich %}
Usage
Basic Example
The sandwich template tag is used to wrap a block of content inside a parent template. The child content will be injected into the parent template at the placeholder {{ sandwich_fixings }}.
Example Parent Template (card_component.html):
<div class="card">
<div class="card-header">
<h2>{{ title }}</h2>
</div>
<div class="card-body">
{{ sandwich_fixings }}
</div>
</div>
Example Usage in Another Template:
{% sandwich "card_component.html" title="Welcome" %}
<p>This content will be wrapped inside a card component.</p>
{% endsandwich %}
Passing Additional Context
Additional key-value pairs can be passed to the sandwich tag, which will be available in the parent template:
{% sandwich "card_component.html" title="User Info" theme="dark" %}
<p>This content inherits the "theme" variable.</p>
{% endsandwich %}
Using a Template Object Instead of a String
You can also pass a Template object instead of a template filename:
{% sandwich some_template_object title="Dynamic Title" %}
<p>Using a template object dynamically.</p>
{% endsandwich %}
How It Works
- The
sandwichtag takes a requiredtemplateargument, which specifies the parent template. - The child block content is rendered separately and passed as
sandwich_fixings. - The parent template is rendered with any additional key-value arguments provided.
Complements Django's Template Inheritance
Django's template inheritance system is great for structuring large applications, but it lacks a built-in way to create deeply nestable, reusable templates or UI components. sandwich fills this gap by allowing content blocks to be dynamically wrapped in different parent templates while maintaining flexibility in context passing. This makes it an excellent tool for building modular front-end structures in Django applications.
Error Handling
- If the
templateargument is missing, aTemplateSyntaxErroris raised. - If
templateis provided both as a positional and keyword argument, an error is raised. - If
templateis not a string or aTemplateobject, an error is raised.
Enjoy building templates with sandwich! 🥪
License
This package is released under the MIT License.
Get Me Some of That
Acknowledgments
This project would be impossible to maintain without the help of our generous contributors
Technology Colophon
Without django and the django dev team, the universe would have fewer rainbows and ponies.
This package was originally created with cookiecutter
and the cookiecutter-powder-pypackage project template.
For Developers
Initialise the development environment using the invoke task
inv tox.venv
Or create it with tox directly
tox d -e dev .venv
Tests
pytest
or
tox r
or run tox environments in parallel using
tox p
Code Style / Linting
$ isort
$ black
$ flake8
Versioning
-
$ bumpver show
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_sandwich_tag-0.2.1.tar.gz.
File metadata
- Download URL: django_sandwich_tag-0.2.1.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f3ef9026ea2edac69c01c48466477d5fee5ae2ae7f3ba3736fbd5a46597e241
|
|
| MD5 |
8bda9a8711a8ce3ef1a7238eb3114128
|
|
| BLAKE2b-256 |
12cef8f72c76ac04d87cb2ab216277560ac060df11b93f9fa0a75a7bd17c2c56
|
Provenance
The following attestation bundles were made for django_sandwich_tag-0.2.1.tar.gz:
Publisher:
publish.yaml on JacobTumak/django-sandwich-tag
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_sandwich_tag-0.2.1.tar.gz -
Subject digest:
6f3ef9026ea2edac69c01c48466477d5fee5ae2ae7f3ba3736fbd5a46597e241 - Sigstore transparency entry: 945621282
- Sigstore integration time:
-
Permalink:
JacobTumak/django-sandwich-tag@2f0ba974fddbdb27d286502c8db9137c2d6757f6 -
Branch / Tag:
refs/tags/0.2.1 - Owner: https://github.com/JacobTumak
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@2f0ba974fddbdb27d286502c8db9137c2d6757f6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_sandwich_tag-0.2.1-py3-none-any.whl.
File metadata
- Download URL: django_sandwich_tag-0.2.1-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d4a9cd3b4a40f2216a30232bdd842a98eb9238e794eccea932dd42482c3c46b
|
|
| MD5 |
36fb51e4a4ebd8709e8f9361947324bc
|
|
| BLAKE2b-256 |
496a48570eac12c2e7f915c64781a72a3a3c618a3ca3dfaa69f55a0ca0469683
|
Provenance
The following attestation bundles were made for django_sandwich_tag-0.2.1-py3-none-any.whl:
Publisher:
publish.yaml on JacobTumak/django-sandwich-tag
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_sandwich_tag-0.2.1-py3-none-any.whl -
Subject digest:
7d4a9cd3b4a40f2216a30232bdd842a98eb9238e794eccea932dd42482c3c46b - Sigstore transparency entry: 945621296
- Sigstore integration time:
-
Permalink:
JacobTumak/django-sandwich-tag@2f0ba974fddbdb27d286502c8db9137c2d6757f6 -
Branch / Tag:
refs/tags/0.2.1 - Owner: https://github.com/JacobTumak
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@2f0ba974fddbdb27d286502c8db9137c2d6757f6 -
Trigger Event:
push
-
Statement type: