Changeset 6580
- Timestamp:
- 10/21/07 10:48:40 (1 year ago)
- Files:
-
- django/trunk/django/templatetags/cache.py (added)
- django/trunk/docs/cache.txt (modified) (1 diff)
- django/trunk/tests/regressiontests/templates/tests.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/docs/cache.txt
r6575 r6580 289 289 minutes. 290 290 291 Template fragment caching 292 ========================= 293 294 If you're after even more control, you can also cache template fragments using 295 the ``cache`` template tag. To give your template access to this tag, put ``{% 296 load cache %}`` near the top of your template. 297 298 The ``{% cache %}`` template tag caches the contents of the block for a given 299 amount of time. It takes at least two arguments: the cache timeout, in 300 seconds, and the name to give the cache fragment. For example:: 301 302 {% load cache %} 303 {% cache 500 sidebar %} 304 .. sidebar .. 305 {% endcache %} 306 307 Sometimes you might want to cache multiple copies of a fragment depending on 308 some dynamic data that appears inside the fragment. For example you may want a 309 separate cached copy of the sidebar used in the previous example for every user 310 of your site. This can be easily achieved by passing additional arguments to 311 the ``{% cache %}`` template tag to uniquely identify the cache fragment:: 312 313 {% load cache %} 314 {% cache 500 sidebar request.user.username %} 315 .. sidebar for logged in user .. 316 {% endcache %} 317 318 If you need more than one argument to identify the fragment that's fine, simply 319 pass as many arguments to ``{% cache %}`` as you need! 320 291 321 The low-level cache API 292 322 ======================= django/trunk/tests/regressiontests/templates/tests.py
r6571 r6580 806 806 'url-fail02' : ('{% url no_such_view %}', {}, ''), 807 807 'url-fail03' : ('{% url regressiontests.templates.views.client no_such_param="value" %}', {}, ''), 808 809 ### CACHE TAG ###################################################### 810 'cache01' : ('{% load cache %}{% cache -1 test %}cache01{% endcache %}', {}, 'cache01'), 811 'cache02' : ('{% load cache %}{% cache -1 test %}cache02{% endcache %}', {}, 'cache02'), 812 'cache03' : ('{% load cache %}{% cache 2 test %}cache03{% endcache %}', {}, 'cache03'), 813 'cache04' : ('{% load cache %}{% cache 2 test %}cache04{% endcache %}', {}, 'cache03'), 814 'cache05' : ('{% load cache %}{% cache 2 test foo %}cache05{% endcache %}', {'foo': 1}, 'cache05'), 815 'cache06' : ('{% load cache %}{% cache 2 test foo %}cache06{% endcache %}', {'foo': 2}, 'cache06'), 816 'cache07' : ('{% load cache %}{% cache 2 test foo %}cache06{% endcache %}', {'foo': 1}, 'cache05'), 817 818 # Raise exception if we dont have at least 2 args, first one integer. 819 'cache08' : ('{% load cache %}{% cache %}{% endcache %}', {}, template.TemplateSyntaxError), 820 'cache09' : ('{% load cache %}{% cache 1 %}{% endcache %}', {}, template.TemplateSyntaxError), 821 'cache10' : ('{% load cache %}{% cache foo bar %}{% endcache %}', {}, template.TemplateSyntaxError), 808 822 } 809 823
