Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fail silently on resolving variables, falling back to settings.TEMPLA…

…TE_STRING_IF_INVALID
  • Loading branch information...
commit eb8723e26b92c0265aa2f0bcdc213daa7e7d35cf 1 parent 498fc42
Jaap Roes authored
Showing with 14 additions and 3 deletions.
  1. +10 −3 fancy_tag/__init__.py
  2. +4 −0 tests/test_app/tests.py
View
13 fancy_tag/__init__.py
@@ -1,7 +1,8 @@
from inspect import getargspec
import re
-from django.template import Node, Variable, generic_tag_compiler
+from django.conf import settings
+from django.template import Node, Variable, VariableDoesNotExist, generic_tag_compiler
from django.template import TemplateSyntaxError, ALLOWED_VARIABLE_CHARS
from django.utils.functional import curry
@@ -79,10 +80,16 @@ def __init__(self, vars_to_resolve, kw_vars_to_resolve, output_var, takes_contex
self.output_var = output_var
self.takes_context = takes_context
+ def safe_resolve(self, var, context):
+ try:
+ return var.resolve(context)
+ except VariableDoesNotExist:
+ return settings.TEMPLATE_STRING_IF_INVALID
+
def render(self, context):
- args = [var.resolve(context) for var in self.vars_to_resolve]
+ args = [self.safe_resolve(var, context) for var in self.vars_to_resolve]
kwargs = dict(
- [(kw, var.resolve(context)) for kw, var in self.kw_vars_to_resolve.items()])
+ [(kw, self.safe_resolve(var, context)) for kw, var in self.kw_vars_to_resolve.items()])
if self.takes_context:
args = [context] + args
View
4 tests/test_app/tests.py
@@ -19,6 +19,10 @@ def testTagWithKwargAndArg(self):
template = Template('{% load example_tags %}{% say_soup "Tomato" size="large" %}')
self.assertEqual(template.render(Context()), 'Tomato Soup (large)')
+ def testContextLookupFailsSilently(self):
+ template = Template('{% load example_tags %}{% say_soup soup %}')
+ self.assertEqual(template.render(Context()), ' Soup')
+
def testArgAfterKwargFails(self):
self.assertRaises(
TemplateSyntaxError,
Please sign in to comment.
Something went wrong with that request. Please try again.