summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Slenders <jonathan@slenders.be>2013-08-14 16:14:32 +0200
committerTim Graham <timograham@gmail.com>2013-08-14 12:40:19 -0400
commitff410565bf1cf1a2905c1ef83db408d455211446 (patch)
tree5f2ecee536c3a2ae1a9bea58d20ec537d669ee71
parent71c491972eecae8783cf46e69fac7e5f9f83fc59 (diff)
Fixed #20709 -- Allowed {% widthratio %} to accept an "as" parameter.
Thanks clay.evil@ for the suggestion.
-rw-r--r--AUTHORS1
-rw-r--r--django/template/defaulttags.py35
-rw-r--r--docs/ref/templates/builtins.txt8
-rw-r--r--docs/releases/1.7.txt3
-rw-r--r--tests/template_tests/tests.py7
5 files changed, 47 insertions, 7 deletions
diff --git a/AUTHORS b/AUTHORS
index cec85c093f..215a06235a 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -539,6 +539,7 @@ answer newbie questions, and generally made Django that much better:
Brenton Simpson <http://theillustratedlife.com>
Jozko Skrablin <jozko.skrablin@gmail.com>
Ben Slavin <benjamin.slavin@gmail.com>
+ Jonathan Slenders
sloonz <simon.lipp@insa-lyon.fr>
Paul Smith <blinkylights23@gmail.com>
Steven L. Smith (fvox13) <steven@stevenlsmith.com>
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
index 5c9490f749..921a3594cc 100644
--- a/django/template/defaulttags.py
+++ b/django/template/defaulttags.py
@@ -458,10 +458,11 @@ class VerbatimNode(Node):
return self.content
class WidthRatioNode(Node):
- def __init__(self, val_expr, max_expr, max_width):
+ def __init__(self, val_expr, max_expr, max_width, asvar=None):
self.val_expr = val_expr
self.max_expr = max_expr
self.max_width = max_width
+ self.asvar = asvar
def render(self, context):
try:
@@ -480,7 +481,13 @@ class WidthRatioNode(Node):
return '0'
except (ValueError, TypeError):
return ''
- return str(int(round(ratio)))
+ result = str(int(round(ratio)))
+
+ if self.asvar:
+ context[self.asvar] = result
+ return ''
+ else:
+ return result
class WithNode(Node):
def __init__(self, var, name, nodelist, extra_context=None):
@@ -1353,20 +1360,34 @@ def widthratio(parser, token):
For example::
- <img src='bar.gif' height='10' width='{% widthratio this_value max_value max_width %}' />
+ <img src="bar.png" alt="Bar"
+ height="10" width="{% widthratio this_value max_value max_width %}" />
If ``this_value`` is 175, ``max_value`` is 200, and ``max_width`` is 100,
the image in the above example will be 88 pixels wide
(because 175/200 = .875; .875 * 100 = 87.5 which is rounded up to 88).
+
+ In some cases you might want to capture the result of widthratio in a
+ variable. It can be useful for instance in a blocktrans like this::
+
+ {% widthratio this_value max_value max_width as width %}
+ {% blocktrans %}The width is: {{ width }}{% endblocktrans %}
"""
bits = token.split_contents()
- if len(bits) != 4:
- raise TemplateSyntaxError("widthratio takes three arguments")
- tag, this_value_expr, max_value_expr, max_width = bits
+ if len(bits) == 4:
+ tag, this_value_expr, max_value_expr, max_width = bits
+ asvar = None
+ elif len(bits) == 6:
+ tag, this_value_expr, max_value_expr, max_width, as_, asvar = bits
+ if as_ != 'as':
+ raise TemplateSyntaxError("Invalid syntax in widthratio tag. Expecting 'as' keyword")
+ else:
+ raise TemplateSyntaxError("widthratio takes at least three arguments")
return WidthRatioNode(parser.compile_filter(this_value_expr),
parser.compile_filter(max_value_expr),
- parser.compile_filter(max_width))
+ parser.compile_filter(max_width),
+ asvar=asvar)
@register.tag('with')
def do_with(parser, token):
diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
index afb6e4b123..1fadf77d50 100644
--- a/docs/ref/templates/builtins.txt
+++ b/docs/ref/templates/builtins.txt
@@ -1108,6 +1108,14 @@ If ``this_value`` is 175, ``max_value`` is 200, and ``max_width`` is 100, the
image in the above example will be 88 pixels wide
(because 175/200 = .875; .875 * 100 = 87.5 which is rounded up to 88).
+.. versionchanged:: 1.7
+
+In some cases you might want to capture the result of ``widthratio`` in a
+variable. It can be useful, for instance, in a :ttag:`blocktrans` like this::
+
+ {% widthratio this_value max_value max_width as width %}
+ {% blocktrans %}The width is: {{ width }}{% endblocktrans %}
+
.. templatetag:: with
with
diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt
index f4bbd058d6..6f6efcb3ca 100644
--- a/docs/releases/1.7.txt
+++ b/docs/releases/1.7.txt
@@ -156,6 +156,9 @@ Minor features
:meth:`~django.contrib.auth.models.User.email_user()` are passed to the
underlying :meth:`~django.core.mail.send_mail()` call.
+* The :ttag:`widthratio` template tag now accepts an "as" parameter to capture
+ the result in a variable.
+
Backwards incompatible changes in 1.7
=====================================
diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py
index aa0283be79..034280f08d 100644
--- a/tests/template_tests/tests.py
+++ b/tests/template_tests/tests.py
@@ -1575,6 +1575,13 @@ class TemplateTests(TransRealMixin, TestCase):
# Test whitespace in filter argument
'widthratio15': ('{% load custom %}{% widthratio a|noop:"x y" b 0 %}', {'a':50,'b':100}, '0'),
+ # Widthratio with variable assignment
+ 'widthratio16': ('{% widthratio a b 100 as variable %}-{{ variable }}-', {'a':50,'b':100}, '-50-'),
+ 'widthratio17': ('{% widthratio a b 100 as variable %}-{{ variable }}-', {'a':100,'b':100}, '-100-'),
+
+ 'widthratio18': ('{% widthratio a b 100 as %}', { }, template.TemplateSyntaxError),
+ 'widthratio19': ('{% widthratio a b 100 not_as variable %}', { }, template.TemplateSyntaxError),
+
### WITH TAG ########################################################
'with01': ('{% with key=dict.key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'),
'legacywith01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'),