Default Layout |
[eluser]derekmichaeljohnson[/eluser]
Hey guys. Brand new to CI, so forgive me if this is a frequently asked question (I tried searching, swear!). I'd like to know if there's a way to have EVERY view loaded within a specific area in a DEFAULT layout. I understand many use this method: Code: $this->load->view('layout/_header'); Code: <html> Does this make sense? And is it possible with CI? I appreciate the feedback!
[eluser]Dam1an[/eluser]
You could use a template library (such as this one by Colin) If you don't need a full blown template library, you could just use a nested view So you create a master view, and then pass the path to the nested view into it (along with any data) If you want to get clever, you can also make it look for a view in views/controller_name/function_name.php and use that if it exists, and even have a hook to call the final view automatically
[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums. You can load views from within views, and simply pass a variable into the container view to load one of the subviews dynamically.
[eluser]derekmichaeljohnson[/eluser]
[quote author="TheFuzzy0ne" date="1242522965"]Welcome to the CodeIgniter forums. You can load views from within views, and simply pass a variable into the container view to load one of the subviews dynamically.[/quote]Great idea! So I've created a "layout" view like so: Code: <html> Code: class Home extends Controller
[eluser]Dam1an[/eluser]
How about not having to give the name of the view (unless you want one other then what it thinks) and not having to call load->view? Easy enough? Create MY_Controller in your libraries Code: <?php if(!defined('BASEPATH')) exit('No direct script access allowed'); Enable hooks in config and add this to your hooks config file Code: // Will automatically call the render method at the end of the function And here's an example controller using it Code: <?php if(!defined('BASEPATH')) exit('No direct script access allowed'); So if you go to site.com/authentication/login (I have it routed from just login) and have a file in the authentication view folder called login.php that view will get loaded automatically, as well as any data you set (You need to you $this->data to set data as its in the parent class) Any questions?
[eluser]CtheB[/eluser]
You could use HMVC to load modules on your page. See HMVC: http://codeigniter.com/wiki/Modular_Extensions_-_HMVC/ See also this topic where i show how you can load all the modules dynamically: http://ellislab.com/forums/viewthread/92212/P90/#575322
[eluser]derekmichaeljohnson[/eluser]
What if I just had the "layout" view load the view which is labeled according to the controller class? Code: class Blog extends Controller Code: <html>
[eluser]Dam1an[/eluser]
Like I showed in my earlier example To get the controller name, use Code: $this->router->class; Code: $this->router->method;
[eluser]derekmichaeljohnson[/eluser]
Damian: I've altered your suggestion (a few posts ago) to look like this: Code: $hook['post_controller'] = array( Code: class MY_Controller extends Controller Code: <body> Code: class Home extends MY_Controller Am I missing something? I really appreciate the help.
[eluser]Dam1an[/eluser]
A few things In the index function, when assigning something to the data array, you need to use Code: $this->data['something'] = 'something'; When you call the view from your render function and pass it the data array, it only goes one level deep (to the master view), which is why I wrapped it in another array when passing it in You might be able to get around that by using Code: $this->load->vars($this->data); I hope that makes sense |
Welcome Guest, Not a member yet? Register Sign In |