Skip to content

Commit

Permalink
Added sum templatetag
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrochh committed May 27, 2014
1 parent 63567ef commit df7c19a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@
*coverage/
dist/
docs/_build/
build/
2 changes: 2 additions & 0 deletions CHANGELOG.txt
@@ -1,5 +1,7 @@
=== (ongoing) ===

- Added sum templatetag


=== 1.49.1 ===

Expand Down
20 changes: 20 additions & 0 deletions django_libs/templatetags/libs_tags.py
Expand Up @@ -504,6 +504,26 @@ def save(context, key, value):
return ''


@register.simple_tag(takes_context=True)
def sum(context, key, value, multiplier=1):
"""
Adds the given value to the total value currently held in ``key``.
Use the multiplier if you want to turn a positive value into a negative
and actually substract from the current total sum.
Usage::
{% sum "MY_TOTAL" 42 -1 %}
{{ MY_TOTAL }}
"""
if key not in context.dicts[0]:
context.dicts[0][key] = 0
context.dicts[0][key] += value * multiplier
return ''


@register.assignment_tag
def set_context(value):
return value
Expand Down
13 changes: 13 additions & 0 deletions docs/libs_tags.rst
Expand Up @@ -402,6 +402,19 @@ elegant when you replace the `if NEEDS_HR` block with::
{% include "django_libs/partials/dynamic_hr.html" %}


sum
---
Adds the given value to the total value currently held in `key`.

Use the multiplier if you want to turn a positive value into a negative
and actually substract from the current total sum.

Usage::

{% sum "MY_TOTAL" 42 -1 %}
{{ MY_TOTAL }}


set_context
-----------

Expand Down

0 comments on commit df7c19a

Please sign in to comment.