Ticket #10043: widthratio-variable.diff

File widthratio-variable.diff, 2.0 KB (added by obeattie, 6 years ago)

Patch to the widthratio template tag to allow variables to be passed for the max_width argument

  • template/defaulttags.py

     
    88except NameError:
    99    from django.utils.itercompat import reversed     # Python 2.3 fallback
    1010
    11 from django.template import Node, NodeList, Template, Context, Variable
     11from django.template import Node, NodeList, Template, Context, Variable, FilterExpression
    1212from django.template import TemplateSyntaxError, VariableDoesNotExist, BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END, SINGLE_BRACE_START, SINGLE_BRACE_END, COMMENT_TAG_START, COMMENT_TAG_END
    1313from django.template import get_library, Library, InvalidTemplateLibrary
    1414from django.conf import settings
     
    396396        try:
    397397            value = self.val_expr.resolve(context)
    398398            maxvalue = self.max_expr.resolve(context)
     399            if isinstance(self.max_width, FilterExpression):
     400                self.max_width = self.max_width.resolve(context)
     401            self.max_width = int(self.max_width)
    399402        except VariableDoesNotExist:
    400403            return ''
     404        except ValueError:
     405            raise TemplateSyntaxError("widthratio final argument must be an integer")
    401406        try:
    402407            value = float(value)
    403408            maxvalue = float(maxvalue)
     
    11031108    if len(bits) != 4:
    11041109        raise TemplateSyntaxError("widthratio takes three arguments")
    11051110    tag, this_value_expr, max_value_expr, max_width = bits
    1106     try:
    1107         max_width = int(max_width)
    1108     except ValueError:
    1109         raise TemplateSyntaxError("widthratio final argument must be an integer")
     1111   
    11101112    return WidthRatioNode(parser.compile_filter(this_value_expr),
    1111                           parser.compile_filter(max_value_expr), max_width)
     1113                          parser.compile_filter(max_value_expr),
     1114                          parser.compile_filter(max_width))
    11121115widthratio = register.tag(widthratio)
    11131116
    11141117#@register.tag
Back to Top