Part of the EllisLab Network
   
2 of 2
2
Loading views within a view file
Posted: 13 April 2010 03:30 AM   [ Ignore ]   [ # 16 ]  
Research Assistant
RankRankRank
Total Posts:  667
Joined  10-21-2008

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.

Profile
 
 
Posted: 13 April 2010 03:40 AM   [ Ignore ]   [ # 17 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  355
Joined  06-05-2008

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.

 Signature 

Zeeshan RasooL
Lahore - Pakistan
http://www.99Points.info


Facebook Wall Script Facebook Style Extract URL data with JQuery Ajax Rating System Script Who Am I

Profile
 
 
Posted: 13 April 2010 03:47 AM   [ Ignore ]   [ # 18 ]  
Research Assistant
RankRankRank
Total Posts:  667
Joined  10-21-2008

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:

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:

$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'); 
Profile
 
 
Posted: 13 April 2010 08:42 AM   [ Ignore ]   [ # 19 ]  
Summer Student
Total Posts:  5
Joined  02-01-2010
Zeeshan Rasool - 13 April 2010 07:12 AM

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:)

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.

Profile
 
 
Posted: 13 April 2010 09:37 AM   [ Ignore ]   [ # 20 ]  
Summer Student
Total Posts:  2
Joined  04-12-2010
adamp1 - 13 April 2010 07:09 AM
jdavidson - 12 April 2010 09:55 PM

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

<?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.

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.

You have a good point with the folder structure argument. That definitely makes it much more beneficial to load all views from the Controller. Thanks for the input.

Profile
 
 
Posted: 15 April 2010 05:21 AM   [ Ignore ]   [ # 21 ]  
Summer Student
Total Posts:  10
Joined  04-13-2010

Ok , i am confused. Why? Because when i

<?php $this->load->view('global/header'); ?> 

in my page view all $data passed to it is available for all widgets and parts i load in the page view ...

The Page view:

<?php $this->load->view('global/header'); ?>
<?php 
echo $template?>    
<?php $this
->load->view('global/footer'); ?> 

The header part (which is included in the page):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/xhtml1-loose.dtd">
<
html >
<
head>
<
title>Codeigniter</title>
<
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <!-- <
link rel="stylesheet" type="text/css" media="screen" href="/theme/css/application.css" /> -->
    <
style>
        .
header { border:solid 1px red}
        
.footer { border:solid 1px green}
    
</style>
</
head>
<
body>
    
    <!-- 
DEBUG: -->
    
    <!-- /
END DEBUG; -->
    <
div class="header">
        <
h3>Header</h3<?php echo $date "test"?>
    
</div

The controller:

class Infopage extends Frontend {

    
function Infopage(){
        parent
::Frontend();
    
}
    
    
function index(){
        $index[
'index_text'"Index lorem ipsum text";
        
$data['template'$this->load->view('infopages/index'$indextrue);
        
$data['date'date("Y-m-d");
        
$this->load->view('page'$data);
    
}

as u can see i am passing the $data[‘date’] to the page -> then page includes header -> in the header i am calling $date ... and everything is perfect.

Why is that? Or maybe the author have different situation and me didnt understand?  red face  If so ... sorry!

Profile
 
 
Posted: 12 January 2012 04:59 AM   [ Ignore ]   [ # 22 ]  
Summer Student
Total Posts:  1
Joined  01-12-2012

I know this thread is almost 2 years old… but it was the first that came up for my Google search and no one has given a definitive answer to:

1. Including children-views that have access to the same variables as the parent-view
2. Not having to call each view in each controller method
3. Setting master content for the sub-views
4. Setting method specific content for the sub-views

I was surprised to find out that not even the Template library suggested here could handle these four tasks, but this basic implementation can.

So here is my solution. It’s quite simple really:

In your controller, do the following
- ‘global’ is a standard string variable, but could be anything
- ‘header’ is a string containing the location of the header sub view
- ‘footer’ is a string containing the location of the footer sub view

public function __construct() {
   parent
::__construct();
   
// Set master content and sub-views
   
$this->load->vars( array(
      
'global' => 'Available to all views',
      
'header' => 'subviews/header',
      
'footer' => 'subviews/footer'
   
));
}
 
public function index() {
   $data[
'title''Index Method';
   
$this->load->view('template'$data);

Then, in your template.php view, wherever you want your sub-views to appear:

<?php $this->load->view($header); ?>
<!-- Other Page Content Here -->
<?php $this->load->view($footer); ?> 

Done like this, $global and $title can be used in all three views (template, subview/header, and subview/footer).

Is there a better solution?

Profile
 
 
Posted: 19 January 2012 12:55 AM   [ Ignore ]   [ # 23 ]  
Summer Student
Total Posts:  2
Joined  01-18-2012

It really MVC structure. If we have the ability to do this using the methods defined in the Gadgets parser or, why we call another point of view.

Profile
 
 
Posted: 08 February 2012 09:55 AM   [ Ignore ]   [ # 24 ]  
Grad Student
Avatar
Rank
Total Posts:  70
Joined  06-12-2011

I think that this is the best solution:

Controller

public function action()
{
   $data 
= array(
     
'view' => 'view_name',
     
'title' => 'Hello World'
   
);

   
$this->load->view('template_tpl'$data);

View

php html 

Template View

php html
<?php $this
->load->view($view); ?>
php 
html 

 

 Signature 

Web developer with open source soul
Blog: porquero.blogspot.com

Profile
 
 
Posted: 06 April 2012 07:05 PM   [ Ignore ]   [ # 25 ]  
Summer Student
Total Posts:  1
Joined  04-06-2012

I think I found the solution

<?php $this->load->view('header');?>
<p>Yak yak yak</p>
<?php $this->load->view('footer');?> 

if you want to pass the variables to the next view, you can use the $_ci_data[‘_ci_vars’]. as below:

<?php $this->load->view('header'$_ci_data['_ci_vars']);?>
<p>Yak yak yak</p>
<?php $this->load->view('footer'$_ci_data['_ci_vars']);?> 

Hope it helps.
cya

Profile
 
 
Posted: 07 April 2012 10:26 PM   [ Ignore ]   [ # 26 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  5417
Joined  06-19-2009

You dont even need to do that!

public function action()
{
   $data 
= array(
     
'view'  => 'view_name',
     
'title' => 'Hello World'
   
);

   
$this->load->vars($data);  // makes vars global to all views!
   
$this->load->view('template_tpl');

 

 Signature 

Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

Profile
 
 
Posted: 02 May 2012 09:32 AM   [ Ignore ]   [ # 27 ]  
Summer Student
Total Posts:  2
Joined  11-04-2011

InsiteFX and others are correct.

This thread came up in google on the subject. I’ve just tweaked my controller and view(s) to follow this scheme. At first, the benefits, not to mention keeping the level of abstraction that MVC provides, wasn’t evident until the code was being written.

Sure the views have

$this->load->view($header); 

calls, but notice that it’s calling not a specific file, but a variable that references a specific file (header.php if you may). The abstraction and thus the benefit comes from setting that var in the controller. Therefore you don’t have to to touch the views to modify what file is being called as the ‘header’ and the view does what it does—output PHP vars and HTML leaving the logic to other components of the framework. Additionally, all vars within the $data object are global to all views (the ‘pTypes’ result set is available for output in the $content sub-view).

...
$data['pTypes'$types;              // assign mysql object results to var
$data['pageTitle''My Page Title';  // assign global var
$data['header''header.php';        // set filename for header sub-view
$data['footer''footer.php';        // set filename for footer sub-view
$data['content''sub/content.php';  // set filename for sub-view
...
$this->load->vars($data);
$this->load->view('dashboard'); // this loads the 'main' view
... 
Profile
 
 
Posted: 20 May 2012 01:31 PM   [ Ignore ]   [ # 28 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  155
Joined  02-23-2011
dmayo2 - 02 May 2012 09:32 AM

InsiteFX and others are correct.

This thread came up in google on the subject. I’ve just tweaked my controller and view(s) to follow this scheme. At first, the benefits, not to mention keeping the level of abstraction that MVC provides, wasn’t evident until the code was being written.

Sure the views have

$this->load->view($header); 

calls, but notice that it’s calling not a specific file, but a variable that references a specific file (header.php if you may). The abstraction and thus the benefit comes from setting that var in the controller. Therefore you don’t have to to touch the views to modify what file is being called as the ‘header’ and the view does what it does—output PHP vars and HTML leaving the logic to other components of the framework. Additionally, all vars within the $data object are global to all views (the ‘pTypes’ result set is available for output in the $content sub-view).

...
$data['pTypes'$types;              // assign mysql object results to var
$data['pageTitle''My Page Title';  // assign global var
$data['header''header.php';        // set filename for header sub-view
$data['footer''footer.php';        // set filename for footer sub-view
$data['content''sub/content.php';  // set filename for sub-view
...
$this->load->vars($data);
$this->load->view('dashboard'); // this loads the 'main' view
... 

Indeed. Taking it a step further, I extended the core controller (http://codeigniter.com/user_guide/general/core_classes.html) and set this $global_data + did load->vars($global_data) inside the __construct(). Now, this code does not need repeating in all the controllers too, just make sure you extend MY_Controller and not CI_Controller.

 Signature 

Circle me on google plus. Follow me on twitter. Look me up on forrst. And check out these superb premium domains that are up for sale.

Profile
 
 
   
2 of 2
2