Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dabapps/django-wrapwith
A Django template tag for wrapping a template block in a reusable enclosing template
https://github.com/dabapps/django-wrapwith
Last synced: 4 days ago
JSON representation
A Django template tag for wrapping a template block in a reusable enclosing template
- Host: GitHub
- URL: https://github.com/dabapps/django-wrapwith
- Owner: dabapps
- License: bsd-2-clause
- Created: 2020-02-14T10:37:15.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-03-20T16:42:27.000Z (9 months ago)
- Last Synced: 2024-11-09T02:19:11.944Z (about 2 months ago)
- Language: Python
- Size: 29.3 KB
- Stars: 5
- Watchers: 17
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
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](https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#include), 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`:
```html
{{ wrapped }}
```Note the special `{{ wrapped }}` variable, which will be replaced with your wrapped content.
Then, in your main page template:
```html
{% load wrapwith %}
welcome to my page
{% wrapwith "wrappers/box.html" with bordercol="red" %}
this is inside a red box
{% endwrapwith %}{% wrapwith "wrappers/box.html" with bordercol="green" %}
this is inside a green box
and here's another paragraph inside the green box
{% endwrapwith %}
```
### 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`:
```python
WRAPWITH_TEMPLATES = {
"wrappers": {
"box": "wrappers/box.html",
},
}
```In your template:
```html
{% wrapwith wrappers.box with bordercol="red" %}
this is inside a red box
{% 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/