from django import template
from django.template import Variable
register = template.Library()
class UserMessageNode(template.Node):
"""The template processor uses this to hold a user message"""
def __init__(self, message):
self.message = Variable(message)
def render(self, context):
try:
message = self.message.resolve(context)
if message[-4:].isdigit():
error_code = message[-4:]
message = message[:-4]
else:
error_code = ''
if error_code == '0000':
return u'<div class="success">' + message + u'</div>'
if error_code == '0001':
return u'<div class="error">' + message + u'</div>'
if error_code == '0002':
return u'<div class="notice">' + message + u'</div>'
else:
return u'<div class="error">' + message + u'</div>'
except template.VariableDoesNotExist:
return ''
@register.tag(name='render_user_message')
def render_user_message(parser, token):
"""A tag that renders a user message differently depending on the severity of it."""
try:
message = token.split_contents()[1]
except ValueError:
raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents.split()[0]
return UserMessageNode(message)
Comments
Why do you use those cryptic error codes instead of prepended SUCCESS:, ERROR: and so on?
#
Using the numbers makes it easy to know which characters are the error code, since there are always 4 characters plus the two brackets around them. Using that knowledge I just count back from the end of the string. I suppose one could use string codes, but I was being a little lazy.
#
Ahh, I said the same thing as david_bgk on your blog, but I'll post here too. Strings are a good idea, david_bgk's uppercase chars are a better ide... your test could then use
isupper()Also, it would be more "pythonic" to use a dict for the CSS class mapping instead of a string of if elif elif elif statements.
Eg
(NB You'll have to cover the possible ValueError from the rsplit if there is no space in message :-)
#
This is how I have chosen to implement my version of this. I made mine a simpletag because I didn't need direct access to the variable or context from the template. From the docs: "When your template tag does not need access to the current context, writing a function to work with the input values and using the simple_tag helper is the easiest way to create a new tag."
#