Welcome Guest, Not a member yet? Register   Sign In
Loading views within a view file
#11

[eluser]skalrynd[/eluser]
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:

$this->load->config('fileName');

my late 2 cents XD
#12

[eluser]Unknown[/eluser]
Thanks for article ... I also studied about it .. your post or much ... thanks
---------------------------
du hoc uc
#13

[eluser]jdavidson[/eluser]
Not sure if this is helpful but another possibility is to use a simple include within the view such as:

Code:
<?php include('system/application/views/header.php'); ?>
[..body code here...]
<?php include('system/application/views/footer.php'); ?>

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.
#14

[eluser]Zeeshan Rasool[/eluser]
You can check parser library which i think is a good solution for your question.
#15

[eluser]adamp1[/eluser]
[quote author="jdavidson" date="1271127323"]Not sure if this is helpful but another possibility is to use a simple include within the view such as:

Code:
<?php include('system/application/views/header.php'); ?>
[..body code here...]
<?php include('system/application/views/footer.php'); ?>

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.[/quote]

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.
#16

[eluser]Zeeshan Rasool[/eluser]
Quote: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 viewSmile
#17

[eluser]mddd[/eluser]
I used to build parts of headers and footers in the Controller and then pass them to the view. But I think it is better to call sub-views like a 'header view' from the main view. Because all of those things are about outputting stuff. So the view is the place to do that.

I didn't know about Loader->vars(). Cool! This makes it even better to do stuff like setting a page title. Because that was one of the irritating things: having to pass info for the header view through the main view.

And of course you can put views in folders to organize them. So for instance you can call $this->load->view('parts/header'); from your main view.
#18

[eluser]Zeeshan Rasool[/eluser]
Header and footer is good to call in main view or index view but if some one trying to call interior views inside a view that's not a good approach. You can track main.php easily but if your site contains a large number of views then its tough to change in future.
#19

[eluser]mddd[/eluser]
I think if you keep things organized it can be a benefit to chop things up. Not TOO much of course. But say you are building an application that show a list of items, and each item has complicated html. You could make that item a separate view. And also things like a pagination that you use on multiple pages of the site.

You could have a controller method:
Code:
function list_page()
{
   $list = $this->product_model->getList();  // this returns an object with items and pagination info in it
   $this->load->vars('pagetitle', 'List of Products');  // this sets the title that can be used in the header view
   $this->load->view('productlist', $list);
}

And the view would be:

Code:
$this->load->view('parts/header');
... some html here that comes above the list ...
foreach($list->items as $item) $this->load->view('list/item', $item);
$this->load->view('list/pagination', $list->pagination);
... some more html under the list ...
$this->load->view('parts/footer');
#20

[eluser]skalrynd[/eluser]
[quote author="Zeeshan Rasool" date="1271160727"]
Quote: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 viewSmile[/quote]

Agreed as well. It does work and it is an optional solution. But in MVC it would be a total unecessary hack against the framework.




Theme © iAndrew 2016 - Forum software by © MyBB