| 1 | from inspect import getargspec |
| 2 | from django.core import template |
| 3 | from django.core.template_loader import render_to_string, get_template |
| 4 | from django.utils.functional import curry |
| 5 | |
| 6 | def gen_compile_func(params, defaults, name, node_class, parser, token): |
| 7 | #look in tags for |
| 8 | bits = token.contents.split()[1:] |
| 9 | bmax = len(params) |
| 10 | def_len = defaults and len(defaults) or 0 |
| 11 | bmin = bmax - def_len |
| 12 | if( len(bits) < bmin or len(bits) > bmax ): |
| 13 | if bmin == bmax: |
| 14 | message = "%s takes %s arguments" % (name, bmin) |
| 15 | else: |
| 16 | message = "%s takes between %s and %s arguments" % (name, bmin, bmax) |
| 17 | raise template.TemplateSyntaxError(message) |
| 18 | return node_class(bits) |
| 19 | |
| 20 | |
| 21 | def simple_tag(func): |
| 22 | (params,_, _, defaults) = getargspec(func) |
| 23 | class TNode(template.Node): |
| 24 | def __init__(self, vars_to_resolve): |
| 25 | #get the vars to resolve |
| 26 | self.vars_to_resolve = vars_to_resolve |
| 27 | |
| 28 | def render(self, context): |
| 29 | resolved_vars = [template.resolve_variable(var, context) |
| 30 | for var in self.vars_to_resolve] |
| 31 | return func(*resolved_vars) |
| 32 | compile_func = curry(gen_compile_func, params, defaults, func.__name__, TNode) |
| 33 | compile_func.__doc__ = func.__doc__ |
| 34 | template.register_tag(func.__name__, compile_func) |
| 35 | return func |
| 36 | |
| 37 | |
| 38 | def inclusion_tag(file_name, context_class=template.Context, takes_context=False): |
| 39 | def dec(func): |
| 40 | (params,_, _, defaults) = getargspec(func) |
| 41 | if takes_context: |
| 42 | if params[0] == 'context': |
| 43 | params = params[1:] |
| 44 | else: |
| 45 | raise template.TemplateSyntaxError("Any tag function decorated with takes_context=True must have a first argument of 'context'" ) |
| 46 | class TNode(template.Node): |
| 47 | def __init__(self, vars_to_resolve): |
| 48 | self.vars_to_resolve = vars_to_resolve |
| 49 | |
| 50 | def render(self, context): |
| 51 | resolved_vars = [template.resolve_variable(var, context) |
| 52 | for var in self.vars_to_resolve] |
| 53 | if takes_context: |
| 54 | args = [context] + resolved_vars |
| 55 | else: |
| 56 | args = resolved_vars |
| 57 | |
| 58 | dict = func(*args) |
| 59 | |
| 60 | if not getattr(self, 'nodelist', False): |
| 61 | t = get_template(file_name) |
| 62 | self.nodelist = t.nodelist |
| 63 | return self.nodelist.render(context_class(dict)) |
| 64 | |
| 65 | compile_func = curry(gen_compile_func, params, defaults, func.__name__, TNode) |
| 66 | compile_func.__doc__ = func.__doc__ |
| 67 | template.register_tag(func.__name__, compile_func) |
| 68 | return func |
| 69 | return dec |
| 70 | |