Private Context Decorator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from django.template.context import Context

def private_context(f):
    """
    Simple decorator which avoids the need to a) copy-and-paste code to force
    context variables into inclusion_tag templates and b) carefully avoid
    inclusion tag variables conflicting with global variables.
    
    Instead each inclusion tag will be called with a *copy* of the provided
    context variable and its results will be merged in to avoid leaking into
    the global context
    """
    from functools import wraps
    
    @wraps(f)
    def private_context_wrapper(context, *args, **kwargs):
        c = Context(context)
        rc = f(c, *args, **kwargs)
        c.update(rc)
        return c

    return private_context_wrapper

More like this

  1. Enhancing template tags with "as variable" syntax by ctrochalakis 4 years, 7 months ago
  2. media_url context variable by marchino 7 years ago
  3. A GET string modifier templatetag by cogat 5 years, 4 months ago
  4. CatchTag - Catching the content and saving it to a variable by limodou 7 years, 1 month ago
  5. testdata tag for templates by showell 4 years, 11 months ago

Comments

(Forgotten your password?)