from django import template

from soapbox.models import Message


register = template.Library()


class MessagesForPageNode(template.Node):
    def __init__(self, url, varname):
        self.url = template.Variable(url)
        self.varname = varname

    def render(self, context):
        try:
            url = self.url.resolve(context)
        except template.VariableDoesNotExist:
            return ''
        context[self.varname] = Message.objects.match(url)
        return ''


def get_messages_for_page(parser, token):
    bits = token.split_contents()
    if len(bits) != 4 or bits[2] != 'as':
        raise template.TemplateSyntaxError("%s syntax must be '{% %s [url] as [varname] %}" % (bits[0], bits[0]))
    return MessagesForPageNode(bits[1], bits[3])

@@ -31,15 +31,9 @@
 rom 
-soapbox
+.
 .mod
@@ -474,16 +474,30 @@
 rn ''%0A%0A%0A
+@register.tag%0A
 def get_
@@ -673,11 +673,8 @@
 be '
-%7B%25 
 %25s %5B
@@ -694,11 +694,8 @@
 ame%5D
- %25%7D
 %22 %25 

Clean up the template tags module a bit.