Code

Ticket #1065: fragment_caching_docs.diff

File fragment_caching_docs.diff, 1.6 KB (added by nick.lane.au@…, 7 years ago)

Added docs

Line 
1=== modified file 'docs/cache.txt'
2--- docs/cache.txt      2007-02-16 12:21:25 +0000
3+++ docs/cache.txt      2007-02-16 15:19:45 +0000
4@@ -277,6 +277,37 @@
5 above example, the result of the ``slashdot_this()`` view will be cached for 15
6 minutes.
7 
8+Fragment caching
9+================
10+
11+If you're after even more control, you can also easily cache template fragments
12+using the ``cache`` template tag. To give your template access to this tag, put
13+``{% load fragment_caching %}`` near the top of your template.
14+
15+The ``{% cache %}`` template tag caches the contents of the block for a given
16+amount of time and takes at least two arguments. The first argument is the
17+cache timeout, in seconds. The second argument is the name to give the cache
18+fragment. For example::
19+
20+    {% load fragment_caching %}
21+    {% cache 500 sidebar %}
22+        .. sidebar ..
23+    {% endcache %}
24+
25+Sometimes you might want to cache multiple copies of a fragment depending on
26+some dynamic data that appears inside the fragment. For example you may want a
27+separate cached copy of the sidebar used in the previous example for every user
28+of your site. This can be easily achieved by passing additional arguments to
29+the ``{% cache %}`` template tag to uniquely identify the cache fragment::
30+
31+    {% load fragment_caching %}
32+    {% cache 500 sidebar request.user.username %}
33+        .. sidebar for logged in user ..
34+    {% endcache %}
35+
36+If you need more than one argument to identify the fragment that's fine, simply
37+pass as many arguments to ``{% cache %}`` as you need!
38+
39 The low-level cache API
40 =======================
41 
42