- Author:
- tensiongyb
- Posted:
- December 5, 2007
- Language:
- Python
- Version:
- .96
- 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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Please login first before commenting.