-
-
Notifications
You must be signed in to change notification settings - Fork 198
Fix/missing context vars #453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -188,6 +188,11 @@ def admin_interface_use_changeform_tabs(adminform, inline_forms): | |
| return has_tabs | ||
|
|
||
|
|
||
| @register.simple_tag(takes_context=True) | ||
| def resolve_variable(context, var_name, default=""): | ||
| return context.get(var_name, default) | ||
|
||
|
|
||
|
|
||
| @register.filter | ||
| def admin_interface_slugify(name): | ||
| return slugify(str(name or "")) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a small thing: please use always the same variable name for coherence. I see sometimes you use
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Acknowledged, and thank you for pointing that out ! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from django.template import Context, Template | ||
| from django.test import SimpleTestCase | ||
|
|
||
|
|
||
| class ResolveVariableTagTests(SimpleTestCase): | ||
| def render_template(self, tpl, context=None): | ||
| if context is None: | ||
| context = {} | ||
| return ( | ||
| Template("{% load admin_interface_tags %}" + tpl) | ||
| .render(Context(context)) | ||
| .strip() | ||
| ) | ||
|
|
||
| def test_returns_existing_variable(self): | ||
| out = self.render_template( | ||
| '{% resolve_variable "myvar" as result %}{{ result }}', {"myvar": "hello"} | ||
| ) | ||
| self.assertEqual(out, "hello") | ||
|
|
||
| def test_returns_default_when_missing(self): | ||
| out = self.render_template( | ||
| '{% resolve_variable "missingvar" as result %}{{ result }}' | ||
| ) | ||
| self.assertEqual(out, "") | ||
|
|
||
| def test_returns_custom_default(self): | ||
| out = self.render_template( | ||
| '{% resolve_variable "missingvar" "fallback" as result %}{{ result }}' | ||
| ) | ||
| self.assertEqual(out, "fallback") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please rename the template tag to
admin_interface_resolve_variable?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
renamed the template tag to admin_interface_resolve_variable as suggested.