Default vars for parser->parse - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Default vars for parser->parse (/showthread.php?tid=56447) |
Default vars for parser->parse - El Forum - 12-20-2012 [eluser]woutr_be[/eluser] I'm fairly new to CodeIgniter, but I'm just learning by doing. I've overwritten the default CI_Controller in order to load views in my general template, like this: Code: class Guest_Controller extends CI_Controller I also want to use this to do some extra authentication checks, like some user are only allowed certain controllers. Now when I use this Guest_Controller, like this: Code: class Homepage extends Guest_Controller { I now want to add some default string to my "body_class", the one that is defined in my Guest_Controller. Is there a way to override parser->parse in my Guest_Controller in orde to do so? This doesn't have to be parser->parse, but I can also just use $this->load->view if it's easier that way. Default vars for parser->parse - El Forum - 12-21-2012 [eluser]Rowan Wilson[/eluser] Are you templating? If not, there's no need to use parser, just stick to using Views. Otherwise the $data array in your Homepage controller will allow you to access those strings directly, e.g. $title, $page_title, $body_classes in your view. Default vars for parser->parse - El Forum - 12-21-2012 [eluser]woutr_be[/eluser] [quote author="rowstu" date="1356099489"]Are you templating? If not, there's no need to use parser, just stick to using Views. Otherwise the $data array in your Homepage controller will allow you to access those strings directly, e.g. $title, $page_title, $body_classes in your view.[/quote] I'm not templating, but then the same question remains, how can I add a default value to $body_classes, I prefer to do this inside my Guest_Controller Default vars for parser->parse - El Forum - 12-21-2012 [eluser]Rowan Wilson[/eluser] Does your Homepage controller work extending Guest_Controller like that? To me it shouldn't. But anyway to answer your question. Inside your Guest_Controller you'll need to make your body_classes variable public and not private, then it will be available to you as $this->body_classes Default vars for parser->parse - El Forum - 12-21-2012 [eluser]woutr_be[/eluser] Why this shouldn't work? I'm not that experienced with CI, but it works yes. But if I make my body_classas public inside Guest_Controller, how will it automatically add the defaults ones to it? Basically when I call $this->load->view, that inside my Guest_Controller, the $body_classes gets added to $data['body_classes'] Default vars for parser->parse - El Forum - 12-21-2012 [eluser]Aken[/eluser] Make your $body_classes property protected or public, otherwise nothing except Guest_Controller can access/modify it. Then, add/modify it in your controllers to set necessary values. Personally, I would leave it in an array format so you don't have to worry about making sure there are spaces around each value. Code: // Guest_Controller: Default vars for parser->parse - El Forum - 12-21-2012 [eluser]woutr_be[/eluser] Thanks, just one more problem, since $body_classes is an array, should I just use implode() to turn it into a string in my 'templates/html_guest', or can I overwrite $this->load->view somehow to do it there first? I would prefer the latter, but just checking what the best options is. EDIT: I just noticed that $body_classes is not available within my 'html_guest.php' Default vars for parser->parse - El Forum - 12-22-2012 [eluser]Aken[/eluser] You'd implode() and include it in whichever data array is appropriate -- either when parsing a template, or in the _output() method. Default vars for parser->parse - El Forum - 12-22-2012 [eluser]woutr_be[/eluser] Ah, got it, that seems to work just fine, thanks! |