blocksetter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class ContextVariableBlockSetter(template.Node):
    def __init__(self, variable, nodelist):
        self.variable = variable
        self.nodelist = nodelist

    def render(self, context):
        context[self.variable] = self.nodelist.render(context)
        return ""

@register.tag
def blockset(parser, node):
    bits = node.contents.split()
    if len(bits) != 2:
          raise template.TemplateSyntaxError, "'%s' tag takes one argument" % bits[0]
    nodelist = parser.parse(('endblockset',))
    parser.delete_first_token()
    return ContextVariableBlockSetter(bits[1], nodelist)

More like this

  1. Create new variables in templates by jmrbcu 5 years, 4 months ago
  2. Complex Form Preview by smagala 4 years, 1 month ago
  3. ExprTag - Calculating python expression and saving the result to a variable by limodou 6 years, 2 months ago
  4. PyCallTag - Directly call python function or attribute of a module by limodou 6 years, 2 months ago
  5. Multilingual Models by Archatas 6 years, 2 months ago

Comments

ubernostrum (on December 10, 2007):

Isn't this what Django's built-in with tag already does?

#

jerzyk (on December 10, 2007):

nope. totally different idea {% blockset url %}http://{{ myhost }}{% url myurl %}{% endblockset %}

then somewhere else [HTML_REMOVED][HTML_REMOVED] etc.

#

(Forgotten your password?)