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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Template variable caching


Go to End


7 Posts   2191 Views

Avatar
MrMuddy

Community Member, 13 Posts

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

Avatar
swaiba

Forum Moderator, 1899 Posts

1 April 2011 at 1:39am

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

Avatar
MrMuddy

Community Member, 13 Posts

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

Avatar
swaiba

Forum Moderator, 1899 Posts

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.

Avatar
MrMuddy

Community Member, 13 Posts

7 April 2011 at 3:29am

OK... understood now !

I'll see what happens

Cheers

Avatar
MrMuddy

Community Member, 13 Posts

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

Avatar
swaiba

Forum Moderator, 1899 Posts

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