2012-04-19 2 views

ответ

1

Вы можете использовать узел, например, так:

inbox.py

from django.template import Library, Node, TemplateSyntaxError 

    class InboxOutput(Node): 
     def __init__(self, varname=None): 
      self.varname = varname 

     def render(self, context): 
      return ... 


    def inbox_count(parser, token): 
     """ 
     Usage:: 

      {% load inbox %} 
      {% inbox_count %} 

      {# or assign the value to a variable: #} 

      {% inbox_count as my_var %} 
      {{ my_var }} 

     """ 
     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', print_inbox_count) 
Смежные вопросы