| | 280 | Fragment caching |
| | 281 | ================ |
| | 282 | |
| | 283 | If you're after even more control, you can also easily cache template fragments |
| | 284 | using the ``cache`` template tag. To give your template access to this tag, put |
| | 285 | ``{% load fragment_caching %}`` near the top of your template. |
| | 286 | |
| | 287 | The ``{% cache %}`` template tag caches the contents of the block for a given |
| | 288 | amount of time and takes at least two arguments. The first argument is the |
| | 289 | cache timeout, in seconds. The second argument is the name to give the cache |
| | 290 | fragment. For example:: |
| | 291 | |
| | 292 | {% load fragment_caching %} |
| | 293 | {% cache 500 sidebar %} |
| | 294 | .. sidebar .. |
| | 295 | {% endcache %} |
| | 296 | |
| | 297 | Sometimes you might want to cache multiple copies of a fragment depending on |
| | 298 | some dynamic data that appears inside the fragment. For example you may want a |
| | 299 | separate cached copy of the sidebar used in the previous example for every user |
| | 300 | of your site. This can be easily achieved by passing additional arguments to |
| | 301 | the ``{% cache %}`` template tag to uniquely identify the cache fragment:: |
| | 302 | |
| | 303 | {% load fragment_caching %} |
| | 304 | {% cache 500 sidebar request.user.username %} |
| | 305 | .. sidebar for logged in user .. |
| | 306 | {% endcache %} |
| | 307 | |
| | 308 | If you need more than one argument to identify the fragment that's fine, simply |
| | 309 | pass as many arguments to ``{% cache %}`` as you need! |
| | 310 | |