Ticket #20709: widthratio-as.patch

File widthratio-as.patch, 5.7 KB (added by jonathanslenders, 11 years ago)

Patch + unit tests + docstring updated.

  • AUTHORS

    commit e57aecd7f5c5f5cbb2dd8dbe8a566159913c896a
    Author: Jonathan Slenders <jonathan@slenders.be>
    Date:   Wed Aug 14 16:14:32 2013 +0200
    
        Fixed #20709.
        Allow {% widthratio %} to accept an "as"-parameter.
    
    diff --git a/AUTHORS b/AUTHORS
    index cec85c0..f18c8bf 100644
    a b answer newbie questions, and generally made Django that much better:  
    570570    Travis Swicegood <travis@domain51.com>
    571571    Pascal Varet
    572572    SuperJared
     573    Jonathan Slenders
    573574    Radek Švarz <http://www.svarz.cz/translate/>
    574575    Swaroop C H <http://www.swaroopch.info>
    575576    Aaron Swartz <http://www.aaronsw.com/>
  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index 5c9490f..921a359 100644
    a b class VerbatimNode(Node):  
    458458        return self.content
    459459
    460460class WidthRatioNode(Node):
    461     def __init__(self, val_expr, max_expr, max_width):
     461    def __init__(self, val_expr, max_expr, max_width, asvar=None):
    462462        self.val_expr = val_expr
    463463        self.max_expr = max_expr
    464464        self.max_width = max_width
     465        self.asvar = asvar
    465466
    466467    def render(self, context):
    467468        try:
    class WidthRatioNode(Node):  
    480481            return '0'
    481482        except (ValueError, TypeError):
    482483            return ''
    483         return str(int(round(ratio)))
     484        result = str(int(round(ratio)))
     485
     486        if self.asvar:
     487            context[self.asvar] = result
     488            return ''
     489        else:
     490            return result
    484491
    485492class WithNode(Node):
    486493    def __init__(self, var, name, nodelist, extra_context=None):
    def widthratio(parser, token):  
    13531360
    13541361    For example::
    13551362
    1356         <img src='bar.gif' height='10' width='{% widthratio this_value max_value max_width %}' />
     1363        <img src="bar.png" alt="Bar"
     1364             height="10" width="{% widthratio this_value max_value max_width %}" />
    13571365
    13581366    If ``this_value`` is 175, ``max_value`` is 200, and ``max_width`` is 100,
    13591367    the image in the above example will be 88 pixels wide
    13601368    (because 175/200 = .875; .875 * 100 = 87.5 which is rounded up to 88).
     1369
     1370    In some cases you might want to capture the result of widthratio in a
     1371    variable. It can be useful for instance in a blocktrans like this::
     1372
     1373        {% widthratio this_value max_value max_width as width %}
     1374        {% blocktrans %}The width is: {{ width }}{% endblocktrans %}
    13611375    """
    13621376    bits = token.split_contents()
    1363     if len(bits) != 4:
    1364         raise TemplateSyntaxError("widthratio takes three arguments")
    1365     tag, this_value_expr, max_value_expr, max_width = bits
     1377    if len(bits) == 4:
     1378        tag, this_value_expr, max_value_expr, max_width = bits
     1379        asvar = None
     1380    elif len(bits) == 6:
     1381        tag, this_value_expr, max_value_expr, max_width, as_, asvar = bits
     1382        if as_ != 'as':
     1383            raise TemplateSyntaxError("Invalid syntax in widthratio tag. Expecting 'as' keyword")
     1384    else:
     1385        raise TemplateSyntaxError("widthratio takes at least three arguments")
    13661386
    13671387    return WidthRatioNode(parser.compile_filter(this_value_expr),
    13681388                          parser.compile_filter(max_value_expr),
    1369                           parser.compile_filter(max_width))
     1389                          parser.compile_filter(max_width),
     1390                          asvar=asvar)
    13701391
    13711392@register.tag('with')
    13721393def do_with(parser, token):
  • docs/ref/templates/builtins.txt

    diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
    index afb6e4b..503aba1 100644
    a b If ``this_value`` is 175, ``max_value`` is 200, and ``max_width`` is 100, the  
    11081108image in the above example will be 88 pixels wide
    11091109(because 175/200 = .875; .875 * 100 = 87.5 which is rounded up to 88).
    11101110
     1111.. versionchanged:: 1.7
     1112
     1113In some cases you might want to capture the result of widthratio in a variable.
     1114It can be useful for instance in a blocktrans like this::
     1115
     1116    {% widthratio this_value max_value max_width as width %}
     1117    {% blocktrans %}The width is: {{ width }}{% endblocktrans %}
     1118
    11111119.. templatetag:: with
    11121120
    11131121with
  • docs/releases/1.7.txt

    diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt
    index f4bbd05..badf78d 100644
    a b Minor features  
    156156  :meth:`~django.contrib.auth.models.User.email_user()` are passed to the
    157157  underlying :meth:`~django.core.mail.send_mail()` call.
    158158
     159* The widthratio template tag now accepts an "as" parameter, to capture the
     160  result in a variable.
     161
    159162Backwards incompatible changes in 1.7
    160163=====================================
    161164
  • tests/template_tests/tests.py

    diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py
    index aa0283b..034280f 100644
    a b class TemplateTests(TransRealMixin, TestCase):  
    15751575            # Test whitespace in filter argument
    15761576            'widthratio15': ('{% load custom %}{% widthratio a|noop:"x y" b 0 %}', {'a':50,'b':100}, '0'),
    15771577
     1578            # Widthratio with variable assignment
     1579            'widthratio16': ('{% widthratio a b 100 as variable %}-{{ variable }}-', {'a':50,'b':100}, '-50-'),
     1580            'widthratio17': ('{% widthratio a b 100 as variable %}-{{ variable }}-', {'a':100,'b':100}, '-100-'),
     1581
     1582            'widthratio18': ('{% widthratio a b 100 as %}', { }, template.TemplateSyntaxError),
     1583            'widthratio19': ('{% widthratio a b 100 not_as variable %}', { }, template.TemplateSyntaxError),
     1584
    15781585            ### WITH TAG ########################################################
    15791586            'with01': ('{% with key=dict.key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'),
    15801587            'legacywith01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'),
Back to Top