Fixed!  I tried putting the contents of my previous templatetags in
another directory and it worked.  Seems silly, though.  Anyhow, now I
have:

project/templatetags/__init__.py
project/templatetags/templatetags/__init__.py
project/templatetags/templatetags/general_tags.py

Also, in case anyone closely scans the code above, I've fixed the
errors in general_tags.py and the tag works dandy!

New general_tags.py:
from django.template import Node, NodeList, resolve_variable
from django.template import TemplateSyntaxError, VariableDoesNotExist
from django.template import Library

register = Library()

class IfGreaterThanNode(Node):
    def __init__(self, var1, var2, nodelist_true, nodelist_false):
        self.var1, self.var2 = var1, var2
        self.nodelist_true, self.nodelist_false = nodelist_true,
nodelist_false

    def __repr__(self):
        return "<IfGreaterThanNode>"

    def render(self, context):
        try:
            val1 = resolve_variable(self.var1, context)
        except VariableDoesNotExist:
            val1 = None
        try:
            val2 = resolve_variable(self.var2, context)
        except VariableDoesNotExist:
            val2 = None
        if (val1 > val2):
            return self.nodelist_true.render(context)
        return self.nodelist_false.render(context)

def do_ifgreaterthan(parser, token):
    bits = list(token.split_contents())
    if len(bits) != 3:
        raise TemplateSyntaxError, "%r takes two arguments" % bits[0]
    end_tag = 'end' + bits[0]
    nodelist_true = parser.parse(('else', end_tag))
    token = parser.next_token()
    if token.contents == 'else':
        nodelist_false = parser.parse((end.tag,))
        parser.delete_first_token()
    else:
        nodelist_false = NodeList()
    return IfGreaterThanNode(bits[1], bits[2], nodelist_true,
nodelist_false)

def ifgreaterthan(parser, token):
    """
    Output the contents of the block if the first argument is greater
than the second argument.

    Examples:

        {% ifgreaterthan person.address_set.count 0 %}
            ...
        {% endifgreaterthan %}

        {% ifgreaterthan person.address_set.count 0 %}
            ...
        {% else %}
            ...
        {% endifgreaterthan %}
    """
    return do_ifgreaterthan(parser, token)
ifgreaterthan = register.tag(ifgreaterthan)


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to