Hi All,
I'm having difficulty with template variables which I initially thought may be an issue with DataObjectmanager (see thread here : http://www.silverstripe.org/dataobjectmanager-module-forum/show/15852)
I've now come across a similar issue when dealing with fairly basic functionality so I'm wondering if there is a caching behaviour at work here.
I have a very simple children control which builds a list of links :
In the relevant page controller :
public function directionArrow() {
$images = array('left.png', 'right.png');
$arrowimage = $images[array_rand($images)];
return $arrowimage;
}
And in the template :
<% control Children %>
<img src="/themes/blackcandy/images/$Parent.directionArrow" alt="arrow"/><span class="wallnav_sign"><a href="$Link" title="Link to $Title case study">$Title</a></span><br/>
<% end_control %>
The first time the function is called, you get a random value... which is then duplicated with every subsequent iteration of the children control.
Now.. reading here : http://doc.silverstripe.org/sapphire/en/reference/advanced-templates suggests that this may be normal behaviour....
"Side effects
All functions that provide data to templates must have no side effects, as the value is cached after first access"
Does anyone know a way around this ? In my mind, the behaviour is wrong. I want a random value every time the function is called, not a cached copy of the first one. It's an autonomous function, so I can't see any rational explanation for the behaviour.
Am I doing something wrong, or is this how it is supposed to work ?
Any ideas appreciated !
Tim