Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions

Template variable caching


Reply

7 Posts   1098 Views

Avatar
MrMuddy

29 March 2011 at 3:17am Community Member, 13 Posts

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

Avatar
swaiba

1 April 2011 at 1:39am Forum Moderator, 1796 Posts

how about moving the direction arrow from "Parent" into the dataobject, then it would not be cached...

Avatar
MrMuddy

7 April 2011 at 3:18am Community Member, 13 Posts

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

Avatar
swaiba

7 April 2011 at 3:26am Forum Moderator, 1796 Posts

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.

Avatar
MrMuddy

7 April 2011 at 3:29am Community Member, 13 Posts

OK... understood now !

I'll see what happens

Cheers

Avatar
MrMuddy

7 April 2011 at 3:36am Community Member, 13 Posts

Thanks swaiba, that worked !

Do you know why the Parent call is cached ? (Always nice to know *why* something happens !)

Cheers

Avatar
swaiba

7 April 2011 at 3:41am Forum Moderator, 1796 Posts

because you are calling the same object each time, therefore cached.
with teh children it's a different object each time