[django-messages] r118 committed - adding a 'as varname' option to the inbox_count templatetag. fixes iss...

4 views
Skip to first unread message

codesite...@google.com

unread,
Oct 12, 2009, 8:42:45 AM10/12/09
to pinax-...@googlegroups.com
Revision: 118
Author: ar...@rcs4u.de
Date: Mon Oct 12 05:37:43 2009
Log: adding a 'as varname' option to the inbox_count templatetag. fixes
issue #40.
http://code.google.com/p/django-messages/source/detail?r=118

Modified:
/trunk/docs/usage.txt
/trunk/messages/templatetags/inbox.py

=======================================
--- /trunk/docs/usage.txt Mon May 25 00:35:38 2009
+++ /trunk/docs/usage.txt Mon Oct 12 05:37:43 2009
@@ -48,3 +48,39 @@
concerns, it's very easy to provide to your own templates. Please see the
:ref:`customization docs <ref-messages-customization>` fore more details.

+
+Templatetags and Context-Processors
+-----------------------------------
+
+Django-messages provides a Templatetag and a Template Context Processor to
+make it easy to print the number of unread messages of a user in the
templates.
+
+To use the Templatetag simply add this to your template::
+
+ {% load inbox %}
+
+Now you can either print the number of unread messages in the users inbox
by
+using::
+
+ {% inbox_count %}
+
+Or you can assign the count to a variable to further process it in the
template::
+
+ {% inbox_count as my_var %}
+ {{ my_var }}
+
+If you want to show the inbox count on every page of your site you could
also
+use the bundled Context Processor to add the value to every Template
Context
+instead of loading the Templatetag. Simply add the Context Processor to the
+TEMPLATE_CONTEXT_PROCESSORS settings in your settings.py::
+
+TEMPLATE_CONTEXT_PROCESSORS = (
+ ...
+ 'messages.context_processors.inbox',
+)
+
+And now every Template Context will contain a variable named
+``messages_inbox_count``, if the user is logged in::
+
+ {{ messages_inbox_count }}
+
=======================================
--- /trunk/messages/templatetags/inbox.py Thu Oct 30 03:09:11 2008
+++ /trunk/messages/templatetags/inbox.py Mon Oct 12 05:37:43 2009
@@ -1,24 +1,45 @@
-from django.template import Library, Node
+from django.template import Library, Node, TemplateSyntaxError

class InboxOutput(Node):
+ def __init__(self, varname=None):
+ self.varname = varname
+
def render(self, context):
try:
user = context['user']
count = user.received_messages.filter(read_at__isnull=True,
recipient_deleted_at__isnull=True).count()
except (KeyError, AttributeError):
count = ''
- return "%s" % (count)
+ if self.varname is not None:
+ context[self.varname] = count
+ return ""
+ else:
+ return "%s" % (count)

def do_print_inbox_count(parser, token):
"""
A templatetag to show the unread-count for a logged in user.
- Prints the number of unread messages in the user's inbox.
+ Returns the number of unread messages in the user's inbox.
Usage::
+
{% load inbox %}
{% inbox_count %}
-
+
+ {# or assign the value to a variable: #}
+
+ {% inbox_count as my_var %}
+ {{ my_var }}
+
"""
- return InboxOutput()
+ bits = token.contents.split()
+ if len(bits) > 1:
+ if len(bits) != 3:
+ raise TemplateSyntaxError, "inbox_count tag takes either no
arguments or exactly two arguments"
+ if bits[1] != 'as':
+ raise TemplateSyntaxError, "first argument to inbox_count tag
must be 'as'"
+ return InboxOutput(bits[2])
+ else:
+ return InboxOutput()

register = Library()
register.tag('inbox_count', do_print_inbox_count)

Reply all
Reply to author
Forward
0 new messages