This way I only have to load the ‘user/register’ view from the controller, but is it good practice? Are all the benifits of using views ( as opposed to just echoing data ) still intact?
The simplest method is to have a $data property that is available in all methods of the given controller. Then you use this property to assign global and controller method specific views variables:
@Colin Williams… I’d submit that using $this->load->vars() CI loader method is “simpler” than adding a class variable to your controller. And it’s shorter. Unless by “simpler” you mean “I prefer it”
This shows a typical use (used in the constructor) of the vars() method of the Loader class. And, incidentally, it’s also the way I prefer to do this sort of thing. Thanks Colin for a nice Template to work from!
I’m doing this way: I’m breaking the whole template into partial views (header, content, footer ...) and I’m using another view called container which binds them together. The content view is passed as a string to the container view by the controller an the rest are just loaded directly by the container. The code looks something like this:
//set the content for the view partial (this is the actual content of the page) $content_data['some_content'] = 'my page content ';
//we load the view partial as a string that will be passed to the container $data['content'] = $this->load->view('some_content_view', $content_data, true);
//set other data that will be used in the container $data['page_title'] = 'page title';
//show the container $this->load->view('container', $data); }
@Colin Williams… I’d submit that using $this->load->vars() CI loader method is “simpler” than adding a class variable to your controller. And it’s shorter. Unless by “simpler” you mean “I prefer it”
This shows a typical use (used in the constructor) of the vars() method of the Loader class. And, incidentally, it’s also the way I prefer to do this sort of thing. Thanks Colin for a nice Template to work from!
Just to confirm…. does this mean that the variables available to views are as follows?
blog/front: header, footer, blogs
template: header, footer, blogs, content
So in other words, $this->load->vars is appended to by any data you submit later?
I know I’m only like 6+ months late on this topic but I found it while doing a forum search.
Wanted to add one other way of approaching this is by taking advantage of the $this->load->config().
I run into the issue with quite a number of variables within my header (meta data, title, etc). And I hate redundancy in my methods like $this->load->view(‘header’). Like the original poster, I’d rather leave that in the view.
Using config to tackle this problem has several payoffs. In many instances header data may remain consistent across each page but in some cases may need to alter one or two (like title). $this->config->item() can be called in any depth of views and is a perfect way of setting some default values to a config file and manipulate those values as you go. Secondly, a single line in your controller does the trick:
As long as you pass all variables needed to the original view, there is no need to “load” additional views since as far as codeigniter is concerned you have only loaded a single view.
As long as you pass all variables needed to the original view, there is no need to “load” additional views since as far as codeigniter is concerned you have only loaded a single view.
I wouldn’t use this method in an MVC framework since there is no need with $this->load->view. Another thing is the paths have to be absolute/relative which mean if you did change the folder structure you have to go update all include statements.
I wouldn’t use this method in an MVC framework since there is no need with $this->load->view. Another thing is the paths have to be absolute/relative which mean if you did change the folder structure you have to go update all include statements.
Agreed ! its really against MVC structure. If we there is ability to do this using parser or defined methods then why we call view in another view:)