Skip to content

Commit

Permalink
Add optional WRAPWITH_TEMPLATES setting to define wrapper template al…
Browse files Browse the repository at this point in the history
…iases
  • Loading branch information
j4mie committed Oct 20, 2021
1 parent cb2e383 commit a734b8a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,27 @@ Then, in your main page template:
</html>
```

That's it!
### 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" %}
<p>this is inside a red box</p>
{% endwrapwith %}
```

Tested on Python 3 with all currently supported Django versions.

Expand Down
5 changes: 5 additions & 0 deletions tests/templates/alias.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% load wrapwith %}

{% wrapwith div %}
hello!
{% endwrapwith %}
5 changes: 5 additions & 0 deletions tests/templates/alias_with_dots.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% load wrapwith %}

{% wrapwith a.b.c %}
hello!
{% endwrapwith %}
12 changes: 11 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.template.loader import render_to_string
from django.test import TestCase
from django.test import override_settings, TestCase


class WrapWithTestCase(TestCase):
Expand Down Expand Up @@ -33,3 +33,13 @@ def test_nested(self):
something after
""",
)

@override_settings(WRAPWITH_TEMPLATES={"div": "wrappers/div.html"})
def test_alias(self):
rendered = render_to_string("alias.html")
self.assertHTMLEqual(rendered, "<div>hello!</div>")

@override_settings(WRAPWITH_TEMPLATES={"a": {"b": {"c": "wrappers/div.html"}}})
def test_dotted_alias(self):
rendered = render_to_string("alias_with_dots.html")
self.assertHTMLEqual(rendered, "<div>hello!</div>")
12 changes: 12 additions & 0 deletions wrapwith/templatetags/wrapwith.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.template import Library
from django.template.loader_tags import do_include

Expand Down Expand Up @@ -27,6 +28,16 @@ def resolve(self, context):
return self.nodelist.render(context)


class ResolveWithAliases:
def __init__(self, template):
self.template = template
self.aliases = getattr(settings, "WRAPWITH_TEMPLATES", {})

def resolve(self, context):
with context.push(self.aliases):
return self.template.resolve(context)


@register.tag(name="wrapwith")
def do_wrapwith(parser, token):
"""
Expand All @@ -36,5 +47,6 @@ def do_wrapwith(parser, token):
include_node = do_include(parser, token)
nodelist = parser.parse(("endwrapwith",))
parser.delete_first_token()
include_node.template = ResolveWithAliases(include_node.template)
include_node.extra_context["wrapped"] = RenderNodelistVariable(nodelist)
return include_node

0 comments on commit a734b8a

Please sign in to comment.