diff --git a/wagtail/wagtailimages/models.py b/wagtail/wagtailimages/models.py index 24fd1f0bf..bd2fc8751 100644 --- a/wagtail/wagtailimages/models.py +++ b/wagtail/wagtailimages/models.py @@ -12,6 +12,7 @@ from django.utils.safestring import mark_safe from django.utils.html import escape from django.conf import settings from django.utils.translation import ugettext_lazy as _ +from django.template.loader import render_to_string from unidecode import unidecode @@ -225,9 +226,9 @@ class AbstractRendition(models.Model): def url(self): return self.file.url - def img_tag(self): + def img_tag(self, attrs={}): return mark_safe( - '%s' % (escape(self.url), self.width, self.height, escape(self.image.title)) + render_to_string('wagtailimages/imagetag.html',{'self' : self, 'attrs' : attrs, }) ) class Meta: diff --git a/wagtail/wagtailimages/templates/wagtailimages/imagetag.html b/wagtail/wagtailimages/templates/wagtailimages/imagetag.html new file mode 100644 index 000000000..d10496739 --- /dev/null +++ b/wagtail/wagtailimages/templates/wagtailimages/imagetag.html @@ -0,0 +1 @@ +{{ self.image.title }} \ No newline at end of file diff --git a/wagtail/wagtailimages/templatetags/image_tags.py b/wagtail/wagtailimages/templatetags/image_tags.py index e59d9cd14..48800d920 100644 --- a/wagtail/wagtailimages/templatetags/image_tags.py +++ b/wagtail/wagtailimages/templatetags/image_tags.py @@ -7,32 +7,46 @@ register = template.Library() # Local cache of filters, avoid hitting the DB filters = {} + @register.tag(name="image") def image(parser, token): - args = token.split_contents() + bits = token.split_contents()[1:] + image_var = bits[0] + filter_spec = bits[1] + bits = bits[2:] - if len(args) == 3: + if len(bits) == 0: # token is of the form {% image self.photo max-320x200 %} - tag_name, image_var, filter_spec = args return ImageNode(image_var, filter_spec) - elif len(args) == 5: + elif len(bits) == 2: # token is of the form {% image self.photo max-320x200 as img %} - tag_name, image_var, filter_spec, as_token, out_var = args - if as_token != 'as': - raise template.TemplateSyntaxError("'image' tag should be of the form {%% image self.photo max-320x200 %%} or {%% image self.photo max-320x200 as img %%}") + if bits[0] == 'as': + return ImageNode(image_var, filter_spec, output_var_name=bits[1]) - return ImageNode(image_var, filter_spec, out_var) + if len(bits) > 0: + # customized attrs + attrs = {} + for bit in bits: + try: + name,value = bit.split('=') + except: + 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 %%}") + if name + attrs[name] = parser.compile_filter(value) # setup to resolve context variables as value - else: - raise template.TemplateSyntaxError("'image' tag should be of the form {%% image self.photo max-320x200 %%} or {%% image self.photo max-320x200 as img %%}") + return ImageNode(image_var, filter_spec, attrs=attrs) + + # something is wrong if we made it this far + 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 %%}") class ImageNode(template.Node): - def __init__(self, image_var_name, filter_spec, output_var_name=None): + def __init__(self, image_var_name, filter_spec, output_var_name=None, attrs={}): self.image_var = template.Variable(image_var_name) self.output_var_name = output_var_name + self.attrs = attrs if filter_spec not in filters: filters[filter_spec], _ = Filter.objects.get_or_create(spec=filter_spec) @@ -66,4 +80,7 @@ class ImageNode(template.Node): return '' else: # render the rendition's image tag now - return rendition.img_tag() + resolved_attrs = {} + for key in self.attrs: + resolved_attrs[key] = self.attrs[key].resolve(context) + return rendition.img_tag(resolved_attrs)