summaryrefslogtreecommitdiff
path: root/tests/template_tests
diff options
context:
space:
mode:
authorCarlton Gibson <carlton.gibson@noumenal.es>2023-09-29 19:02:19 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-10-02 16:16:43 +0200
commit35bbb2c9c01882b1d77b0b8c737ac646144833d4 (patch)
treef10240809715d7855db319d1dbad4eaadadb0dba /tests/template_tests
parentf4e72e6523e6968d9628dfbff914ab57dbf19e6b (diff)
Fixed #34883 -- Allowed template tags to set extra data on templates.
By setting a value in the `parser.extra_data` mapping, template tags pass additional data out of the parsing context. Any extra data set is exposed on the template via the matching `.extra_data` attribute. Library authors should use a key to namespace extra data. The 'django' namespace is reserved for internal use.
Diffstat (limited to 'tests/template_tests')
-rw-r--r--tests/template_tests/templatetags/custom.py7
-rw-r--r--tests/template_tests/tests.py9
2 files changed, 16 insertions, 0 deletions
diff --git a/tests/template_tests/templatetags/custom.py b/tests/template_tests/templatetags/custom.py
index 0937085c8c..8d1130ae78 100644
--- a/tests/template_tests/templatetags/custom.py
+++ b/tests/template_tests/templatetags/custom.py
@@ -1,4 +1,5 @@
from django import template
+from django.template.base import TextNode
from django.template.defaultfilters import stringfilter
from django.utils.html import escape, format_html
from django.utils.safestring import mark_safe
@@ -216,3 +217,9 @@ class CounterNode(template.Node):
count = self.count
self.count = count + 1
return str(count)
+
+
+@register.tag("extra_data")
+def do_extra_data(parser, token):
+ parser.extra_data["extra_data"] = "CUSTOM_DATA"
+ return TextNode("")
diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py
index d9bdd6e68f..14df81669b 100644
--- a/tests/template_tests/tests.py
+++ b/tests/template_tests/tests.py
@@ -140,6 +140,15 @@ class TemplateTestMixin:
if self.debug_engine:
self.assertEqual(e.exception.template_debug["during"], "{% badtag %}")
+ def test_compile_tag_extra_data(self):
+ """Custom tags can pass extra data back to template."""
+ engine = self._engine(
+ app_dirs=True,
+ libraries={"custom": "template_tests.templatetags.custom"},
+ )
+ t = engine.from_string("{% load custom %}{% extra_data %}")
+ self.assertEqual(t.extra_data["extra_data"], "CUSTOM_DATA")
+
def test_render_tag_error_in_extended_block(self):
"""Errors in extended block are displayed correctly."""
e = self._engine(app_dirs=True)