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:
|
570 | 570 | Travis Swicegood <travis@domain51.com> |
571 | 571 | Pascal Varet |
572 | 572 | SuperJared |
| 573 | Jonathan Slenders |
573 | 574 | Radek Švarz <http://www.svarz.cz/translate/> |
574 | 575 | Swaroop C H <http://www.swaroopch.info> |
575 | 576 | Aaron Swartz <http://www.aaronsw.com/> |
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
index 5c9490f..921a359 100644
a
|
b
|
class VerbatimNode(Node):
|
458 | 458 | return self.content |
459 | 459 | |
460 | 460 | class WidthRatioNode(Node): |
461 | | def __init__(self, val_expr, max_expr, max_width): |
| 461 | def __init__(self, val_expr, max_expr, max_width, asvar=None): |
462 | 462 | self.val_expr = val_expr |
463 | 463 | self.max_expr = max_expr |
464 | 464 | self.max_width = max_width |
| 465 | self.asvar = asvar |
465 | 466 | |
466 | 467 | def render(self, context): |
467 | 468 | try: |
… |
… |
class WidthRatioNode(Node):
|
480 | 481 | return '0' |
481 | 482 | except (ValueError, TypeError): |
482 | 483 | 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 |
484 | 491 | |
485 | 492 | class WithNode(Node): |
486 | 493 | def __init__(self, var, name, nodelist, extra_context=None): |
… |
… |
def widthratio(parser, token):
|
1353 | 1360 | |
1354 | 1361 | For example:: |
1355 | 1362 | |
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 %}" /> |
1357 | 1365 | |
1358 | 1366 | If ``this_value`` is 175, ``max_value`` is 200, and ``max_width`` is 100, |
1359 | 1367 | the image in the above example will be 88 pixels wide |
1360 | 1368 | (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 %} |
1361 | 1375 | """ |
1362 | 1376 | 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") |
1366 | 1386 | |
1367 | 1387 | return WidthRatioNode(parser.compile_filter(this_value_expr), |
1368 | 1388 | parser.compile_filter(max_value_expr), |
1369 | | parser.compile_filter(max_width)) |
| 1389 | parser.compile_filter(max_width), |
| 1390 | asvar=asvar) |
1370 | 1391 | |
1371 | 1392 | @register.tag('with') |
1372 | 1393 | def do_with(parser, token): |
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
|
1108 | 1108 | image in the above example will be 88 pixels wide |
1109 | 1109 | (because 175/200 = .875; .875 * 100 = 87.5 which is rounded up to 88). |
1110 | 1110 | |
| 1111 | .. versionchanged:: 1.7 |
| 1112 | |
| 1113 | In some cases you might want to capture the result of widthratio in a variable. |
| 1114 | It 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 | |
1111 | 1119 | .. templatetag:: with |
1112 | 1120 | |
1113 | 1121 | with |
diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt
index f4bbd05..badf78d 100644
a
|
b
|
Minor features
|
156 | 156 | :meth:`~django.contrib.auth.models.User.email_user()` are passed to the |
157 | 157 | underlying :meth:`~django.core.mail.send_mail()` call. |
158 | 158 | |
| 159 | * The widthratio template tag now accepts an "as" parameter, to capture the |
| 160 | result in a variable. |
| 161 | |
159 | 162 | Backwards incompatible changes in 1.7 |
160 | 163 | ===================================== |
161 | 164 | |
diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py
index aa0283b..034280f 100644
a
|
b
|
class TemplateTests(TransRealMixin, TestCase):
|
1575 | 1575 | # Test whitespace in filter argument |
1576 | 1576 | 'widthratio15': ('{% load custom %}{% widthratio a|noop:"x y" b 0 %}', {'a':50,'b':100}, '0'), |
1577 | 1577 | |
| 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 | |
1578 | 1585 | ### WITH TAG ######################################################## |
1579 | 1586 | 'with01': ('{% with key=dict.key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'), |
1580 | 1587 | 'legacywith01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'), |