Skip to main content

A django template tag that simplifies nested templates.

Reason this release was yanked:

no app config - django won't find templatetags

Project description

Django Sandwich Tags

A django template tag that simplifies nested templates.


PyPI Version Tests Code style: black

Version: 0.1.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.

  1. Ensure your Django project is set up and running.
  2. Add the sandwich_tag module to one of your installed Django apps.
  3. 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

  1. The sandwich tag takes a required template argument, which specifies the parent template.
  2. The child block content is rendered separately and passed as sandwich_fixings.
  3. 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 template argument is missing, a TemplateSyntaxError is raised.
  • If template is provided both as a positional and keyword argument, an error is raised.
  • If template is not a string or a Template object, an error is raised.

License

This package is released under the MIT License.


Enjoy building templates with sandwich! 🥪

Quick Start

  1. Install the sandwich package from PyPI

    $ pip install sandwich
    
  2. Add 'sandwich' to INSTALLED_APPS:

    INSTALLED_APPS = [
        ...,
        "sandwich",
           ...,
    ]
    

Get Me Some of That

MIT License

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

Or install the dev requirements with pip

pip install -r reqirements_dev.txt

Tests

pytest

or

tox r

or run tox environments in parallel using

tox p

Code Style / Linting

$ isort
$ black
$ flake8

Versioning

Build / Deploy Automation

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_sandwich_tag-0.1.1.tar.gz (8.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_sandwich_tag-0.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file django_sandwich_tag-0.1.1.tar.gz.

File metadata

  • Download URL: django_sandwich_tag-0.1.1.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for django_sandwich_tag-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4f953fb1c132443369630a8b132e520ee4c19734421fbc69a1ad8cdead733ab3
MD5 1714ee503b2f68d68e41c6308437fbdf
BLAKE2b-256 75b0e15ff79cbb183f929877bd77e30f13ee4501000c7baae4b4a79b803a9c18

See more details on using hashes here.

File details

Details for the file django_sandwich_tag-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_sandwich_tag-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 75de03ac4bbd23fcaff50c849aa855d1256df3543ce9b4b9307e77118c93bf5e
MD5 f5c691554cca9c1c40ad226873f759ac
BLAKE2b-256 5dd2b4247469667d384d60470fd167f3a9b0f2b21fb72900e88641b3a849c5a8

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