On Thu, Apr 14, 2011 at 2:49 PM, cricket <zijn.digi...@gmail.com> wrote:

> On Thu, Apr 14, 2011 at 11:00 AM, Brian Sweeney <eclecticg...@gmail.com>
> wrote:
> > I think even though some of the content is state-sensitive the page could
> > still benefit from view caching. It does present some difficulties,
> though,
> > which is why I'm here.
> >
> > But even if I take out the state-sensitive aspects of the page I'm still
> > running into user-specific inconsistencies in the cached view. The worst
> > example is if I access the page initially as an admin vs. normal user.
> The
> > cached page is completely broken in the latter case, but works fine in
> the
> > former.
>
> <cake:nocache>
> <?php
> if ( (bool) $this->Session->read('Auth.User.admin') )
> {
>        echo $this->element('comments/list_admin');
> }
> else
> {
>        echo $this->element('comments/list');
> }
> ?>
> </cake:nocache>
>
...

>  > But can I access, for example, the authentication component? I pass
> along
> > the instantiation to the view (i.e. $this->set('Auth', $this->Auth)) so
> that
> > I can display some restricted content inline on the page.
>
> When a user logs in, Auth saves the User record to the session with
> the key Auth.User. In a controller, you access this with:
>
> $this->Auth->user('field_name')
>
> In a view:
>
> $this->Session->read('Auth.User.field_name')
>
> To check whether the user is logged in:
>
> if ($this->Session->read('Auth.User.id <http://auth.user.id/>')) { }
>

This might help with one of the problems I've been facing. I'll give it a
try.

> Sorry, I meant component-instantiated variables. Though, as I mentioned
> > above, I'm also doing some things with the auth component in the view.
> > Should I be accessing this information by another means?
>
> Variables to be handed to the view are set() in the controller. In a
> component:
>
> $this->Controller->set('foo', 'bar');
>
> Assuming your component has something like:
>
> public $Controller;
>
> function startup(&Controller)
> {
>    $this->Controller = $Controller;
> }
>

I don't have anything like the startup function (I assume that's part of the
default controller class?). But yes, this is how I'm passing variables to
the view. Except in some of the non-cached code where I'm using
requestAction() to get data I need.


> > Perhaps. I'd like to know more about the caching process, though, so I
> have
> > a better feel for what's possible and what's not. This seems like a bug
> to
> > me because the nocache content is being cached after the first iteration.
> > But I realize it could also just be a limitation of how the document is
> > parsed.
>
> It's more to do with how the loop is being parsed. Put the nocache
> tags outside of the loop.
>

I'd like to place the nocache tags around the whole loop, but this is where
my simplified example breaks down. The loop contains both cacheable and
non-cacheable content. I guess a good analogy to my page would be a product
list where next to each product is an icon indicating whether or not you've
placed it in your cart. So my loop actually looks closer to this (again,
somewhat simplified):

<?php
echo '<table>';
foreach ($products as $product) {
  echo '<tr><td>', $product['Product']['name'] , '</td><td>';
  echo (in_array($product['Product']['id'], $user_cart_items) ? 'Yes' :
'No');
  echo '</td></tr>';
}
echo '</table>';
?>

So what I want to do is place the nocache tags around the Yes/No echo inside
the foreach loop. As I mentioned in an earlier email I was able to work
around my particular issue by using page elements. I'm also working around
one of the limitations in view caching by echoing out some PHP code so that
I can pass the current product id to my page component. So a functional
caching example based using the code from above would look like this:

<?php
echo '<table>';
foreach ($products as $product) {
  echo '<tr><td>', $product['Product']['name'] , '</td><td>';
  $product_id = $product['Product']['id'];
  echo "<?php $product_id = $product_id; ?>"; ?><cake:nocache><?php
  echo $this->element('cart-control', array('product_id' => $item_id,
'user_cart_items' => $user_cart_items)); ?></cake:nocache><?php
  echo '</td></tr>';
}
echo '</table>';

$user_cart_items is instantiated at the top of the page in a nocache block.
Ugly and confusing, yes? But it works. ;)

Since I do have some code that works with the loop, the discussion is mostly
academic as far as that problem is concerned.


>  >>
> http://nuts-and-bolts-of-cakephp.com/2011/02/05/make-your-cakephp-app-ridiculously-faster-with-view-caching/
> >
> > One of my first find when I started doing this ;)
>
> Teknoid's site is an excellent resource. I've learned lots from it. Be
> aware, though, that Cake has changed a lot in the past couple of
> years. So info you find online may be outdated sometimes.
>

Yes, I noticed that particular article was for 1.3, whereas I'm still using
1.2. But the advice still seems relevant.

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to