Mod to allow simple_tag to access context

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    def simple_tag(self,func,takes_context=False):
        params, xx, xxx, defaults = getargspec(func)

        if takes_context:
            if params[0] == 'context':
                params = params[1:]
            else:
                raise TemplateSyntaxError("Any tag function with takes_context set must have a first argument of 'context'")

        class SimpleNode(Node):
            def __init__(self, vars_to_resolve):
                self.vars_to_resolve = map(Variable, vars_to_resolve)

            def render(self, context):
                resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
                if takes_context:
                    return func(context, *resolved_vars)
                else:
                    return func(*resolved_vars)

        compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)
        compile_func.__doc__ = func.__doc__
        self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
        return func

More like this

  1. testdata tag for templates by showell 4 years, 11 months ago
  2. Paginator TemplateTag by trbs 6 years ago
  3. Jinja2 integration + application specific functions/filters/tests by hasenj 5 years, 6 months ago
  4. Run Django as a FastCGI authorizer by cme 5 years, 5 months ago
  5. a simple tag with context by dsblank 4 years, 3 months ago

Comments

(Forgotten your password?)