Skip to content

Commit 32f512c

Browse files
authored
Merge pull request #3 from dabapps/alias-setting
Add optional WRAPWITH_TEMPLATES setting to define wrapper template aliases
2 parents cb2e383 + 4242110 commit 32f512c

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,27 @@ Then, in your main page template:
4040
</html>
4141
```
4242

43-
That's it!
43+
### Optional: aliasing templates
44+
45+
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.
46+
47+
In your `settings.py`:
48+
49+
```python
50+
WRAPWITH_TEMPLATES = {
51+
"wrappers": {
52+
"box": "wrappers/box.html",
53+
},
54+
}
55+
```
56+
57+
In your template:
58+
59+
```html
60+
{% wrapwith wrappers.box with bordercol="red" %}
61+
<p>this is inside a red box</p>
62+
{% endwrapwith %}
63+
```
4464

4565
Tested on Python 3 with all currently supported Django versions.
4666

tests/templates/alias.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% load wrapwith %}
2+
3+
{% wrapwith div %}
4+
hello!
5+
{% endwrapwith %}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% load wrapwith %}
2+
3+
{% wrapwith a.b.c %}
4+
hello!
5+
{% endwrapwith %}

tests/tests.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.template.loader import render_to_string
2-
from django.test import TestCase
2+
from django.test import override_settings, TestCase
33

44

55
class WrapWithTestCase(TestCase):
@@ -33,3 +33,13 @@ def test_nested(self):
3333
something after
3434
""",
3535
)
36+
37+
@override_settings(WRAPWITH_TEMPLATES={"div": "wrappers/div.html"})
38+
def test_alias(self):
39+
rendered = render_to_string("alias.html")
40+
self.assertHTMLEqual(rendered, "<div>hello!</div>")
41+
42+
@override_settings(WRAPWITH_TEMPLATES={"a": {"b": {"c": "wrappers/div.html"}}})
43+
def test_dotted_alias(self):
44+
rendered = render_to_string("alias_with_dots.html")
45+
self.assertHTMLEqual(rendered, "<div>hello!</div>")

wrapwith/templatetags/wrapwith.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.conf import settings
12
from django.template import Library
23
from django.template.loader_tags import do_include
34

@@ -27,6 +28,21 @@ def resolve(self, context):
2728
return self.nodelist.render(context)
2829

2930

31+
class ResolveWithAliases:
32+
"""
33+
Wraps a FilterExpression and injects the WRAPWITH_TEMPLATES alias
34+
dictionary into its context before resolving the variable name.
35+
"""
36+
37+
def __init__(self, template):
38+
self.template = template
39+
self.aliases = getattr(settings, "WRAPWITH_TEMPLATES", {})
40+
41+
def resolve(self, context):
42+
with context.push(self.aliases):
43+
return self.template.resolve(context)
44+
45+
3046
@register.tag(name="wrapwith")
3147
def do_wrapwith(parser, token):
3248
"""
@@ -36,5 +52,6 @@ def do_wrapwith(parser, token):
3652
include_node = do_include(parser, token)
3753
nodelist = parser.parse(("endwrapwith",))
3854
parser.delete_first_token()
55+
include_node.template = ResolveWithAliases(include_node.template)
3956
include_node.extra_context["wrapped"] = RenderNodelistVariable(nodelist)
4057
return include_node

0 commit comments

Comments
 (0)