Variable resolving URL template tag

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from django.template import defaulttags, VariableDoesNotExist, Variable

class ResolvingURLNode(defaulttags.URLNode):
    def render(self, context):
        original_view_name = self.view_name
        try:
            self.view_name = Variable(self.view_name).resolve(context)
        except VariableDoesNotExist:
            pass
        ret = super(defaulttags.URLNode, self).render(context)
        # restore view_name in case this node is reused (e.g in a loop) in 
        # which case the variable might resolve to something else in the next iteration)
        self.view_name = original_view_name
        return ret

defaulttags.URLNode = ResolvingURLNode

More like this

  1. Resolve URLs to view name by UloPe 4 years, 3 months ago
  2. A templatetag to insert the output of another view (or local URL) by jamesgpearce 4 years ago
  3. Support for {% macro %} tags in templates, version 2 by mludvig 5 years, 10 months ago
  4. template tag for highlighting currently active page by adunar 4 years, 8 months ago
  5. Manipulate URL query strings using context variables using a template tag by JHsaunders 2 years, 8 months ago

Comments

t_rybik (on April 29, 2010):

It should be:

 super(ResolvingURLNode, self).render(context)

but besides that all cool.

#

(Forgotten your password?)