2019-11-18 05:58:33 +01:00
---
2014-10-13 09:49:30 +02:00
title: Caching
2020-05-21 04:36:27 +02:00
summary: How template variables are cached.
2019-11-18 05:58:33 +01:00
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.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
private $counter = 0;
2014-10-13 09:49:30 +02:00
2017-10-27 04:38:27 +02:00
public function Counter()
{
$this->counter += 1;
2014-10-13 09:49:30 +02:00
2017-10-27 04:38:27 +02:00
return $this->counter;
}
2017-08-03 02:51:32 +02:00
```
2014-10-13 09:49:30 +02:00
2017-08-03 02:51:32 +02:00
```ss
2017-10-27 04:38:27 +02:00
$Counter, $Counter, $Counter
2014-10-13 09:49:30 +02:00
2017-10-27 04:38:27 +02:00
// returns 1, 1, 1
2017-08-03 02:51:32 +02:00
```
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
2020-05-21 04:36:27 +02:00
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
2020-05-21 04:36:27 +02:00
Example:
2017-08-03 02:51:32 +02:00
```ss
2020-05-21 04:36:27 +02:00
< % cached $CacheKey if $CacheCondition %>
$CacheableContent
2017-10-27 04:38:27 +02:00
< % end_cached %>
2017-08-03 02:51:32 +02:00
```