Django

Code

Changeset 6580

Show
Ignore:
Timestamp:
10/21/07 10:48:40 (1 year ago)
Author:
mtredinnick
Message:

Fixed #1065 -- Added a "cache" template tag. Thanks, Ian Maurer and, particularly, Nick Lane.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/cache.txt

    r6575 r6580  
    289289minutes. 
    290290 
     291Template fragment caching 
     292========================= 
     293 
     294If you're after even more control, you can also cache template fragments using 
     295the ``cache`` template tag. To give your template access to this tag, put ``{% 
     296load cache %}`` near the top of your template. 
     297 
     298The ``{% cache %}`` template tag caches the contents of the block for a given 
     299amount of time. It takes at least two arguments: the cache timeout, in 
     300seconds, and the name to give the cache fragment. For example:: 
     301 
     302    {% load cache %} 
     303    {% cache 500 sidebar %} 
     304        .. sidebar .. 
     305    {% endcache %} 
     306 
     307Sometimes you might want to cache multiple copies of a fragment depending on 
     308some dynamic data that appears inside the fragment. For example you may want a 
     309separate cached copy of the sidebar used in the previous example for every user 
     310of your site. This can be easily achieved by passing additional arguments to 
     311the ``{% 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 
     318If you need more than one argument to identify the fragment that's fine, simply 
     319pass as many arguments to ``{% cache %}`` as you need! 
     320 
    291321The low-level cache API 
    292322======================= 
  • django/trunk/tests/regressiontests/templates/tests.py

    r6571 r6580  
    806806            'url-fail02' : ('{% url no_such_view %}', {}, ''), 
    807807            '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), 
    808822        } 
    809823