{% renderonce %} template tag

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
@register.tag
def renderonce(parser, token):
    nodelist = parser.parse(('endrenderonce',))
    parser.delete_first_token()
    return RenderOnceNode(nodelist)

class RenderOnceNode(template.Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist
    def render(self, context):
        k = type(self)
        if k not in context.render_context:
            context.render_context[k] = {}
        output = self.nodelist.render(context)
        if output in context.render_context[k]:
            output = ''
        else:
            context.render_context[k][output] = True
        return output

More like this

  1. Cache Backend using memcached including prefix settings by mojemeno123 11 months ago
  2. Private Context Decorator by acdha 3 years, 9 months ago
  3. Automatically slugify slug fields in your models by Aliquip 6 years, 2 months ago
  4. Extended Paginator by davisp 5 years, 9 months ago
  5. head inclusion middleware by bowdengm 4 years, 3 months ago

Comments

Samus_ (on June 16, 2010):

so you did it! great :) just a few comments:

you forgot to add the imports here, I think it'll be better to have a snippet that can work without modifications when you download it instead of having only the relevant parts.

also you should mention that it requires Django 1.2 (for the render_context)

#

Samus_ (on June 19, 2010):

oh nice! they added a dropdown to select the django version the script is for, but it looks it defaulted to 1.2 which I'm fairly sure most of the scripts on the website aren't compatible with.

#

(Forgotten your password?)