Skip to main content

django-wrapwith

A Django template tag for wrapping a template block in a reusable enclosing template.

Provides a block tag called wrapwith which behaves exactly like the built-in include tag, but injects the contents of the block into the included template.

It is intended to make wrapper markup reusable, encouraging you to break your template into "components" which might have a particular enclosing structure but varying contents. It is particularly useful with design systems that provide components (cards, blockquotes, accordians etc) that have reusable structure but arbitrary content.

A toy example: imagine your design includes a box component which has a coloured border, but can contain any other markup inside it.

First, create a wrapper template, wrappers/box.html:

<div style="border: 1px solid {{ bordercol }}">
  {{ wrapped }}
</div>

Note the special {{ wrapped }} variable, which will be replaced with your wrapped content.

Then, in your main page template:

{% load wrapwith %}

<html>
  <body>
    <h1>welcome to my page</h1>

    {% wrapwith "wrappers/box.html" with bordercol="red" %}
      <p>this is inside a red box</p>
    {% endwrapwith %}

    {% wrapwith "wrappers/box.html" with bordercol="green" %}
      <p>this is inside a green box</p>
      <p>and here's another paragraph inside the green box</p>
    {% endwrapwith %}
  </body>
</html>

Optional: aliasing templates

If you find writing out the full template path every time you use a component too verbose, you can define a dictionary of "aliases" in your Django settings, using the setting name WRAPWITH_TEMPLATES. This dictionary can be nested. You can then use a dotted path into this dictionary in your templates.

In your settings.py:

WRAPWITH_TEMPLATES = {
  "wrappers": {
    "box": "wrappers/box.html",
  },
}

In your template:

{% wrapwith wrappers.box with bordercol="red" %}
  <p>this is inside a red box</p>
{% endwrapwith %}

Tested on Python 3 with all currently supported Django versions.

Installation

pip install django-wrapwith

Then add wrapwith to your INSTALLED_APPS.

Code of conduct

For guidelines regarding the code of conduct when contributing to this repository please review https://www.dabapps.com/open-source/code-of-conduct/

Release files for django-wrapwith 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-wrapwith 0.0.2
File Size Uploaded
django-wrapwith-0.0.2.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-wrapwith 0.0.2
File Interpreter ABI Platform
django_wrapwith-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.2 kB

Release files / django-wrapwith-0.0.2.tar.gz

Download URL django-wrapwith-0.0.2.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
50ef60c4333dc8c315645c13a2368f2f00c821933f4591fcbe6c2262f1ab3ca3
BLAKE2b-256 checksum
How to use checksums
80acc3578161344437b597583be45ae71cfd6832e0965deab04dce6c584a5375
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Release files / django_wrapwith-0.0.2-py3-none-any.whl

Download URL django_wrapwith-0.0.2-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
03ca3091b11fc5e0d0e3a11efad555e9062a0c4bf5f4f7e9aef4f857ac112b5c
BLAKE2b-256 checksum
How to use checksums
d7b578ab114e5ee854495879f888d54c4e6aa85f4b92f8e4ca3dda02422c52ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page