Jump to:

22976 Posts in 11493 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » Problem with 'uncached' inside a Layout?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 618 Views
  • inCharge
    Avatar
    Community Member
    102 Posts

    Problem with 'uncached' inside a Layout? Link to this post

    Partial cacheing: http://doc.silverstripe.org/sapphire/en/reference/partial-caching

    The uncached tag doesn't seem to work when used inside a layout to cancel a cached tag in the main Page.ss. Or am I doing something wrong?

    I have a cached tag in tutorial/templates/Page.ss and an uncached tag in tutorial/templates/Layout/Page.ss, but he contents of the uncached tag are still cached.

    There is a workaround, which is to close the cached tag before $Layout and open another cached tag inside the layout file if necessary, then a nested uncached tag works.
    Maybe it's good practice never to enclose $Layout in a cached tag, and to consider all layouts on a case-by-case basis. Whatever, I don't think the behaviour I'm seeing is intended. I can't see any reference to this issue in trac.

    This is how to reproduce the problem using the tutorial theme. Or is the problem that I've misunderstood how the cached and uncached tags are used?

    _config.php

    SS_Cache::set_cache_lifetime('any', 24 * 60 * 60);

    Page.php

       function CurrentDateTime() {
          return date('Y-m-d H:i:s');
       }

    Page.ss
    Note the cached tag wrapped around the $Layout

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" >
       <head>
          <% base_tag %>
          <link rel="stylesheet" type="text/css" href="tutorial/css/layout.css" />
          <link rel="stylesheet" type="text/css" href="tutorial/css/typography.css" />
          <link rel="stylesheet" type="text/css" href="tutorial/css/form.css" />
       </head>
       <body>
          <div id="Main">
    <% cached 'Outer', ID, LastEdited, SiteConfig.LastEdited %>
    <% include CacheStats %>
    $Layout
    <% end_cached %>
          </div>
          $SilverStripeNavigator
       </body>
    </html>

    Layout/Page.ss

    <h1>Inside layout</h1>
    <% include CacheStats %>

    Includes/CacheStats.ss
    Note the uncached tag wrapped around the first two timestamps in the table

          <div id="ContentContainer">
             <h2>Cache Stats:</h2>
             <table>
                <% uncached %>
                <tr>
                   <td>Now</td>
                   <td>$CurrentDateTime</td>
                </tr>
                <tr>
                   <td>LastEdited</td>
                   <td>$LastEdited</td>
                </tr>
                <% end_uncached %>
                <tr>
                   <td>Cached</td>
                   <td>$CurrentDateTime</td>
                </tr>
             </table>
          </div>

          

    The page type is used to create a page in the CMS. Before viewing the published page, delete all files in silverstripe-cache/cache

    The first time the page is viewed, both sets of times are the same.
    The second time the page is viewed, the two values of Now are different, which should never happen. All times inside the layout are the same as before, they have been cached, even though they are in a cached tag.

    Cache Stats:
       Now      2011-09-26 18:07:42
       LastEdited   2011-09-25 21:59:32
       Cached      2011-09-26 18:07:18

    Inside layout

    Cache Stats:
       Now      2011-09-26 18:07:18
       LastEdited   2011-09-25 21:59:32
       Cached      2011-09-26 18:07:18

    The Now value should always show the current time, because it is uncached, right?

  • tv
    Avatar
    Community Member
    43 Posts

    Re: Problem with 'uncached' inside a Layout? Link to this post

    Hey inCharge, I am having this same problem. Did you ever figure out what was going on with this?

  • inCharge
    Avatar
    Community Member
    102 Posts

    Re: Problem with 'uncached' inside a Layout? Link to this post

    The work-around I ended up using is to end the Page header's 'cached' statement just before the $Layout, and have another 'cached' statement for the footer, and another inside the layout..

    This does mean that you end up with a lot of files in the cache, especially on a big site, but apart fropm that, it isn't as bad as it sounds. It makes sense that caching should be considered on a case-by-case basis for each layout. And it's often the case that the footer is the same for all pages, so using a cache ID like...

    <% cached 'Footer', SiteConfig.LastEdited %>

    ...means one cached version of the footer is shared by every page on the site.

  • tv
    Avatar
    Community Member
    43 Posts

    Re: Problem with 'uncached' inside a Layout? Link to this post

    Thanks for the reply. That's what I was thinking - adding cache blocks inside each layout template.

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: Problem with 'uncached' inside a Layout? Link to this post

    Might be something like this: the SSViewer class, that renders the main page, will create a new SSViewer instance to render the $Layout subtemplate. So the main SSViewer has no control over what happens in the $Layout template. It just includes whatever HTML is returned and when $Layout falls within a cached block, it never even bothers to create that second viewer.

    So what's left is your solution, I guess. Just wanted to know why

    618 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.