Ticket #2799: inclusion_tag.diff
File inclusion_tag.diff, 2.6 KB (added by , 18 years ago) |
---|
-
django/template/__init__.py
839 839 self.tag(func.__name__, compile_func) 840 840 return func 841 841 842 def inclusion_tag(self, file_name, context_class=Context, takes_context=False ):842 def inclusion_tag(self, file_name, context_class=Context, takes_context=False, cache_timeout=0, cleanup_sender=None, cleanup_signal=None): 843 843 def dec(func): 844 844 params, xx, xxx, defaults = getargspec(func) 845 845 if takes_context: … … 848 848 else: 849 849 raise TemplateSyntaxError, "Any tag function decorated with takes_context=True must have a first argument of 'context'" 850 850 851 use_cache = cache_timeout > 0 852 if use_cache: 853 from django.core.cache import cache 854 cache_id = "%s.%s" % (func.__module__, func.func_name) 855 851 856 class InclusionNode(Node): 852 857 def __init__(self, vars_to_resolve): 853 858 self.vars_to_resolve = vars_to_resolve 854 859 855 860 def render(self, context): 861 862 if use_cache: 863 output = cache.get(cache_id) 864 if output is not None: 865 return output 866 856 867 resolved_vars = [resolve_variable(var, context) for var in self.vars_to_resolve] 857 868 if takes_context: 858 869 args = [context] + resolved_vars … … 865 876 from django.template.loader import get_template 866 877 t = get_template(file_name) 867 878 self.nodelist = t.nodelist 868 return self.nodelist.render(context_class(dict)) 879 output = self.nodelist.render(context_class(dict)) 880 if use_cache: 881 cache.set(cache_id, output, cache_timeout) 882 return output 869 883 870 884 compile_func = curry(generic_tag_compiler, params, defaults, func.__name__, InclusionNode) 871 885 compile_func.__doc__ = func.__doc__ 872 886 self.tag(func.__name__, compile_func) 887 888 if use_cache and cleanup_sender and cleanup_signal: 889 def cleanup(): 890 cache.delete(cache_id) 891 from django.dispatch import dispatcher 892 dispatcher.connect(cleanup, signal=cleanup_signal, sender=cleanup_sender, weak=False) 873 893 return func 874 894 return dec 875 895