Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Template variable caching

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

Page: 1
Go to End
Author Topic: 1004 Views
  • MrMuddy
    Avatar
    Community Member
    13 Posts

    Template variable caching Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Template variable caching Link to this post

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

  • MrMuddy
    Avatar
    Community Member
    13 Posts

    Re: Template variable caching Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Template variable caching Link to this post

    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.

  • MrMuddy
    Avatar
    Community Member
    13 Posts

    Re: Template variable caching Link to this post

    OK... understood now !

    I'll see what happens

    Cheers

  • MrMuddy
    Avatar
    Community Member
    13 Posts

    Re: Template variable caching Link to this post

    Thanks swaiba, that worked !

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

    Cheers

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Template variable caching Link to this post

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

    1004 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.