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'
' + message + u'
'
if error_code == '0001':
return u'' + message + u'
'
if error_code == '0002':
return u'' + message + u'
'
else:
return u'' + message + u'
'
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)