Friday, October 2, 2009

Re: Access Classes In Layout Help??

On Fri, Oct 2, 2009 at 10:55 PM, hahmadi82 <hahmadi82@gmail.com> wrote:
>
>
> Ok so I actually didn't create a layout for each view from my controllers.
> Instead, all the views use the same layout, which is the default.ctp. Is
> that incorrect? From my understanding, the default layout is loaded by every
> view and that's why I have my navigation bar in it.  It seems that I can
> only access those "set" variables from something like
> views/layouts/cars/index.ctp but not views/layouts/default.ctp. Is there a
> difference between the model layouts and the default layout?

You *almost* have it. Once again:

When a controller's action is run, its render() method is called
automatically (yes, you can call it yourself but please ignore that
for now). When that happens, Cake will use the View class to render
the view template for that action. These templates are in
app/views/controller_name_ending_in_s/action_name.ctp

Usually, the view template contains some HTML that you have included,
along with some variables. Those variables are passed to the View
class through the controller's $viewVars class variable. When you call
$this->set('foo', 'bar'), you are passing the value, 'bar' to the
controller's $viewVars array with a key, 'foo'.

When the controller's $viewVars is handed off to the View class, it
extracts them, essentially creating a var named $foo that contains the
value 'bar'.

Now, after the View has finished using the view template to render
something to output it creates a variable called, $content_for_layout.

It then renders the layout template. That's a file in
app/views/layout/name_of_your_layout.ctp. If you don't specify a
layout, Cake uses 'default'.

Inside the layout template is (should be) a variable named ...
$content_for_layout. This is where the contents of your rendered view
are written to the layout.

So, try this: In one of your controller actions, add $this->set('foo', 'bar');

In your app/views/layouts/default.ctp add this, just above $content_for_layout

echo $foo;

You should see 'bar' in there, somewhere. View source and search for
it, because your CSS may hide it.

Anyway, perhaps you should post the relevant part of your layout file
and the controller action.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: