Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

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

Project description


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 }}

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

Then, in your main page template:

{% load wrapwith %}

    <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 %}

That's it!

Tested on Python 3 with all currently supported Django versions.


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

Project details

Download files

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

Files for django-wrapwith, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size django_wrapwith-0.0.1-py3-none-any.whl (5.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size django-wrapwith-0.0.1.tar.gz (3.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page