Now I see! So the set variables change depending on which view is showing within the default.ctp. If I add this car action to the app_controller (instead of car model) and set the variables there, will all the views have access to that variable? How can I make a global "set" variable that comes from a specific query?
brian-263 wrote: > > > On Fri, Oct 2, 2009 at 10:55 PM, hahmadi82 <hahmad...@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. > > > > > -- View this message in context: http://www.nabble.com/Access-Classes-In-Layout-Help---tp25706283p25726631.html Sent from the CakePHP mailing list archive at Nabble.com. --~--~---------~--~----~------------~-------~--~----~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---