3212 Posts in 847 Topics by 809 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 839 Views |
-
Template variable caching

29 March 2011 at 3:17am
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
-
Re: Template variable caching

1 April 2011 at 1:39am
how about moving the direction arrow from "Parent" into the dataobject, then it would not be cached...
-
Re: Template variable caching

7 April 2011 at 3:18am
Thanks for the response.
Just to clarify... the example here isn't using a DataObject.. this is just a basic page template / page control. I encountered a similar issue with another page that does use the DOM, and hence my initial thoughts were that it was a DOM issue. Now I have come across it elsewhere, I'm thinking that this is template caching behaviour instead.
I guess what I'm trying to get to the bottom of is whether this is happening by design or if it's a bug. If it is by design, then (despite thinking it is wrong), I can go and find another solution. If it's a bug, then I can obviously act on it accordingly.
Cheers
-
Re: Template variable caching

7 April 2011 at 3:26am
Just to clarify <% control Children %> returns a set of Pages, that are basically DataObject, but yes Pages would have been better.
by calling Parent it will be cached, allow the function to be called within each child Page it won't be.
-
Re: Template variable caching

7 April 2011 at 3:29am
OK... understood now !
I'll see what happens
Cheers
-
Re: Template variable caching

7 April 2011 at 3:36am
Thanks swaiba, that worked !
Do you know why the Parent call is cached ? (Always nice to know *why* something happens !)
Cheers
-
Re: Template variable caching

7 April 2011 at 3:41am
because you are calling the same object each time, therefore cached.
with teh children it's a different object each time
| 839 Views | ||
|
Page:
1
|
Go to Top |


