Login

function_tag

Author:
tensiongyb
Posted:
December 5, 2007
Language:
Python
Version:
.96
Tags:
tag function
Score:
5 (after 5 ratings)

register any python function as a tag with an optional name.

usage:

in templatetags: @function_tag() def foo(arg): return do_something(arg)

in template: {% foo arg %} or {% foo arg as variable %}{{ variable.bar }}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from django import template
import re
import functools
register = template.Library()

# register any python function as a tag with an optional name
r_func = re.compile(r'(.*?)\s+as\s+(\w+)', re.DOTALL)
def register_function_tag(func, name=None):
    class FunctionNode(template.Node):
        def __init__(self, arg_string, var_name):
            self.arg_string = arg_string
            self.var_name = var_name

        def render(self, context):
            clist = list(context)
            clist.reverse()
            d = {"func": func}
            for c in clist:
                d.update(c)
            var = eval("func(%s)" % self.arg_string, d)
            if self.var_name is not None:
                context[self.var_name] = var 
                return ''
            else:
                return str(var)

    def do_function(parser, token):
        try:
            tag_name, arg_string = token.contents.split(None, 1)
        except ValueError:
            raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents.split()[0]
        m = r_func.search(arg_string)
        if m is not None:
            func_string, var_name = m.groups()
        else:
            if not arg_string:
                raise template.TemplateSyntaxError, "%r tag requires one argument" % tag_name
            func_string, var_name = arg_string, None
        return FunctionNode(func_string, var_name)

    if name is not None:
        register.tag(name, do_function)
    else:
        register.tag(func.__name__, do_function)

def function_tag(name=None):
    return functools.partial(register_function_tag, name=name)


# for example
@function_tag()
def foo(arg):
    return do_something(arg)

# in template
# {% foo arg %} or
# {% foo arg as variable %} {{ variable.bar }}

More like this

  1. Tag that parses args and kwargs by fivethreeo 6 years, 6 months ago
  2. Flatpage Suggester Template tag for 404 templates by bradmontgomery 5 years, 2 months ago
  3. ached decorator for functions by pawnhearts 5 years, 3 months ago
  4. CallTag - Just like include, but can pass parameters to it by limodou 8 years, 2 months ago
  5. decorator for decorators with optional args by homunq 3 years, 4 months ago

Comments

Please login first before commenting.