Welcome to the Cookbook

loading...

7.2.5 Marking Non-Cached Content in Views

There is no translation yet for this section. Please help out and translate this.. More information about translations

There will be times when you don't want an entire view cached. For example, certain parts of the page may look different whether a user is currently logged in or browsing your site as a guest.

To indicate blocks of content that are not to be cached, wrap them in <cake:nocache> </cake:nocache> like so:

<cake:nocache>
<?php if ($session->check('User.name')) : ?>
    Welcome, <?php echo $session->read('User.name')?>.
<?php else: ?>
    <?php echo $html->link('Login', 'users/login')?>
<?php endif; ?>
</cake:nocache>
  1. <cake:nocache>
  2. <?php if ($session->check('User.name')) : ?>
  3. Welcome, <?php echo $session->read('User.name')?>.
  4. <?php else: ?>
  5. <?php echo $html->link('Login', 'users/login')?>
  6. <?php endif; ?>
  7. </cake:nocache>

It should be noted that once an action is cached, the controller method for the action will not be called - otherwise what would be the point of caching the page. Therefore, it is not possible to wrap <cake:nocache> </cake:nocache> around variables which are set from the controller as they will be null.