silverstripe-framework/docs/en/02_Developer_Guides/01_Templates/07_Caching.md

46 lines
961 B
Markdown
Raw Normal View History

---
2014-10-13 09:49:30 +02:00
title: Caching
summary: How template variables are cached.
icon: rocket
---
2014-10-13 09:49:30 +02:00
# Caching
## Object caching
All functions that provide data to templates must have no side effects, as the value is cached after first access. For
example, this controller method will not behave as you might imagine.
```php
private $counter = 0;
2014-10-13 09:49:30 +02:00
public function Counter()
{
$this->counter += 1;
2014-10-13 09:49:30 +02:00
return $this->counter;
}
```
2014-10-13 09:49:30 +02:00
```ss
$Counter, $Counter, $Counter
2014-10-13 09:49:30 +02:00
// returns 1, 1, 1
```
2014-10-13 09:49:30 +02:00
When we render `$Counter` to the template we would expect the value to increase and output `1, 2, 3`. However, as
`$Counter` is cached at the first access, the value of `1` is saved.
## Partial caching
Partial caching is a feature that allows caching of a portion of a page as a single string value. For more details read [its own documentation](partial_template_caching).
2014-10-13 09:49:30 +02:00
Example:
```ss
<% cached $CacheKey if $CacheCondition %>
$CacheableContent
<% end_cached %>
```