diff --git a/wagtail/wagtailimages/templatetags/wagtailimages_tags.py b/wagtail/wagtailimages/templatetags/wagtailimages_tags.py index 385366a2a7..9f2a4343c1 100644 --- a/wagtail/wagtailimages/templatetags/wagtailimages_tags.py +++ b/wagtail/wagtailimages/templatetags/wagtailimages_tags.py @@ -9,13 +9,13 @@ register = template.Library() @register.tag(name="image") def image(parser, token): bits = token.split_contents()[1:] - image_var = bits[0] + image_expr = parser.compile_filter(bits[0]) filter_spec = bits[1] bits = bits[2:] if len(bits) == 2 and bits[0] == 'as': # token is of the form {% image self.photo max-320x200 as img %} - return ImageNode(image_var, filter_spec, output_var_name=bits[1]) + return ImageNode(image_expr, filter_spec, output_var_name=bits[1]) else: # token is of the form {% image self.photo max-320x200 %} - all additional tokens # should be kwargs, which become attributes @@ -25,14 +25,14 @@ def image(parser, token): name, value = bit.split('=') except ValueError: raise template.TemplateSyntaxError("'image' tag should be of the form {% image self.photo max-320x200 [ custom-attr=\"value\" ... ] %} or {% image self.photo max-320x200 as img %}") - attrs[name] = parser.compile_filter(value) # setup to resolve context variables as value + attrs[name] = parser.compile_filter(value) # setup to resolve context variables as value - return ImageNode(image_var, filter_spec, attrs=attrs) + return ImageNode(image_expr, filter_spec, attrs=attrs) class ImageNode(template.Node): - def __init__(self, image_var_name, filter_spec, output_var_name=None, attrs={}): - self.image_var = template.Variable(image_var_name) + def __init__(self, image_expr, filter_spec, output_var_name=None, attrs={}): + self.image_expr = image_expr self.output_var_name = output_var_name self.attrs = attrs self.filter_spec = filter_spec @@ -44,7 +44,7 @@ class ImageNode(template.Node): def render(self, context): try: - image = self.image_var.resolve(context) + image = self.image_expr.resolve(context) except template.VariableDoesNotExist: return '' diff --git a/wagtail/wagtailimages/tests/tests.py b/wagtail/wagtailimages/tests/tests.py index 66365f36a5..bba1e1f9ca 100644 --- a/wagtail/wagtailimages/tests/tests.py +++ b/wagtail/wagtailimages/tests/tests.py @@ -43,6 +43,10 @@ class TestImageTag(TestCase): self.assertTrue('height="300"' in result) self.assertTrue('alt="Test image"' in result) + def test_image_tag_none(self): + result = self.render_image_tag(None, "width-500") + self.assertEqual(result, '') + def render_image_tag_as(self, image, filter_spec): temp = template.Template('{% load wagtailimages_tags %}{% image image_obj ' + filter_spec + ' as test_img %}') context = template.Context({'image_obj': image}) @@ -70,6 +74,16 @@ class TestImageTag(TestCase): self.assertTrue('class="photo"' in result) self.assertTrue('title="my wonderful title"' in result) + def render_image_tag_with_filters(self, image): + temp = template.Template('{% load wagtailimages_tags %}{% image image_primary|default:image_alternate width-400 %}') + context = template.Context({'image_primary': None, 'image_alternate': image}) + return temp.render(context) + + def test_image_tag_with_filters(self): + result = self.render_image_tag_with_filters(self.image) + self.assertTrue('width="400"' in result) + self.assertTrue('height="300"' in result) + class TestMissingImage(TestCase): """