Ticket #6201: cache_tag2.diff
File cache_tag2.diff, 4.8 KB (added by , 16 years ago) |
---|
-
django/templatetags/cache.py
1 from django.template import Library, Node, TemplateSyntaxError 2 from django.template import resolve_variable 1 from django.template import Library, Node, Variable, TemplateSyntaxError, VariableDoesNotExist 3 2 from django.core.cache import cache 4 3 from django.utils.encoding import force_unicode 5 4 … … 13 12 self.vary_on = vary_on 14 13 15 14 def render(self, context): 15 try: 16 self.expire_time = int(self.expire_time) 17 except ValueError: 18 try: 19 self.expire_time = int(Variable(self.expire_time).resolve(context)) 20 except (ValueError, TypeError, VariableDoesNotExist): 21 raise TemplateSyntaxError(u"Cannot resolve cache timeout! '%s' must be an integer." % self.expire_time) 16 22 # Build a unicode key for this fragment and all vary-on's. 17 23 cache_key = u':'.join([self.fragment_name] + \ 18 [force_unicode( resolve_variable(var,context)) for var in self.vary_on])24 [force_unicode(Variable(var).resolve(context)) for var in self.vary_on]) 19 25 value = cache.get(cache_key) 20 26 if value is None: 21 27 value = self.nodelist.render(context) … … 48 54 tokens = token.contents.split() 49 55 if len(tokens) < 3: 50 56 raise TemplateSyntaxError(u"'%r' tag requires at least 2 arguments." % tokens[0]) 51 try: 52 expire_time = int(tokens[1]) 53 except ValueError: 54 raise TemplateSyntaxError(u"First argument to '%r' must be an integer (got '%s')." % (tokens[0], tokens[1])) 55 return CacheNode(nodelist, expire_time, tokens[2], tokens[3:]) 57 return CacheNode(nodelist, tokens[1], tokens[2], tokens[3:]) 56 58 57 59 register.tag('cache', do_cache) -
docs/cache.txt
334 334 {% endcache %} 335 335 336 336 It's perfectly fine to specify more than one argument to identify the fragment. 337 Simply pass as many arguments to ``{% cache %}`` as you need. 337 Simply pass as many arguments to ``{% cache %}`` as you need. The cache timeout 338 can also be a template variable. 338 339 339 340 The low-level cache API 340 341 ======================= -
tests/regressiontests/templates/tests.py
882 882 'cache04' : ('{% load cache %}{% cache 2 test %}cache04{% endcache %}', {}, 'cache03'), 883 883 'cache05' : ('{% load cache %}{% cache 2 test foo %}cache05{% endcache %}', {'foo': 1}, 'cache05'), 884 884 'cache06' : ('{% load cache %}{% cache 2 test foo %}cache06{% endcache %}', {'foo': 2}, 'cache06'), 885 'cache07' : ('{% load cache %}{% cache 2 test foo %}cache06{% endcache %}', {'foo': 1}, 'cache05'), 885 'cache07' : ('{% load cache %}{% cache 2 test foo %}cache07{% endcache %}', {'foo': 1}, 'cache05'), 886 'cache08' : ('{% load cache %}{% cache time test foo %}cache08{% endcache %}', {'foo': 2, 'time': 2}, 'cache06'), 887 'cache09' : ('{% load cache %}{% cache time test foo %}cache09{% endcache %}', {'foo': 3, 'time': -1}, 'cache09'), 888 'cache10' : ('{% load cache %}{% cache time test foo %}cache10{% endcache %}', {'foo': 3, 'time': -1}, 'cache10'), 886 889 887 # Raise exception if we dont have at least 2 args, first one integer. 888 'cache08' : ('{% load cache %}{% cache %}{% endcache %}', {}, template.TemplateSyntaxError), 889 'cache09' : ('{% load cache %}{% cache 1 %}{% endcache %}', {}, template.TemplateSyntaxError), 890 'cache10' : ('{% load cache %}{% cache foo bar %}{% endcache %}', {}, template.TemplateSyntaxError), 890 # Raise exception if we dont have at least 2 args, first one resolved to integer. 891 'cache11' : ('{% load cache %}{% cache %}{% endcache %}', {}, template.TemplateSyntaxError), 892 'cache12' : ('{% load cache %}{% cache 1 %}{% endcache %}', {}, template.TemplateSyntaxError), 893 'cache13' : ('{% load cache %}{% cache foo bar %}{% endcache %}', {}, template.TemplateSyntaxError), 894 'cache14' : ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': 'fail'}, template.TemplateSyntaxError), 895 'cache15' : ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': []}, template.TemplateSyntaxError), 891 896 892 897 ### AUTOESCAPE TAG ############################################## 893 898 'autoescape-tag01': ("{% autoescape off %}hello{% endautoescape %}", {}, "hello"),