EllisLab text mark
Advanced Search
2 of 3
2
   
Loading views within a view file
Posted: 13 April 2010 04:30 AM   [ # 16 ]   [ Rating: 0 ]
Joined: 2008-10-21
643 posts

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.

 
Posted: 13 April 2010 04:40 AM   [ # 17 ]   [ Rating: 0 ]
Avatar
Joined: 2008-06-05
333 posts

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

 
Posted: 13 April 2010 04:47 AM   [ # 18 ]   [ Rating: 0 ]
Joined: 2008-10-21
643 posts

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'); 
 
Posted: 13 April 2010 09:42 AM   [ # 19 ]   [ Rating: 0 ]
Joined: 2010-02-01
3 posts
Zeeshan Rasool - 13 April 2010 08: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.

 
Posted: 13 April 2010 10:37 AM   [ # 20 ]   [ Rating: 0 ]
Joined: 2010-04-12
2 posts
adamp1 - 13 April 2010 08:09 AM
jdavidson - 12 April 2010 10: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.

 
Posted: 15 April 2010 06:21 AM   [ # 21 ]   [ Rating: 0 ]
Joined: 2010-04-13
10 posts

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!

 
Posted: 12 January 2012 04:59 AM   [ # 22 ]   [ Rating: 0 ]
Joined: 2012-01-12
1 posts

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?

 
Posted: 19 January 2012 12:55 AM   [ # 23 ]   [ Rating: 0 ]
Joined: 2012-01-18
2 posts

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.

 
Posted: 08 February 2012 09:55 AM   [ # 24 ]   [ Rating: 0 ]
Avatar
Joined: 2011-06-12
69 posts

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

 
Posted: 06 April 2012 08:05 PM   [ # 25 ]   [ Rating: 0 ]
Joined: 2012-04-06
1 posts

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

 
Posted: 07 April 2012 11:26 PM   [ # 26 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6587 posts

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 

Certified State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

Skype: insitfx

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

 
Posted: 02 May 2012 10:32 AM   [ # 27 ]   [ Rating: 0 ]
Joined: 2011-11-04
4 posts

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
... 
 
Posted: 20 May 2012 02:31 PM   [ # 28 ]   [ Rating: 0 ]
Avatar
Joined: 2011-02-23
182 posts
dmayo2 - 02 May 2012 10: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://ellislab.com/codeigniter/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.

 
Posted: 01 July 2012 09:30 AM   [ # 29 ]   [ Rating: 0 ]
Joined: 2012-06-24
17 posts

I use a custom autoloaded model to allow views called from a Template model.

<?php
class Template extends CI_Model
{
 
 
protected $header;
 protected 
$footer;
 
 
// constructor required by all models
 
function __construct()
 
{
  parent
::__construct(); 
  
$this->header 'themes/default/header';
  
$this->footer 'themes/default/footer';
 
}
 
 
function setHeader($view)
 
{
  $this
->header $view;
 
}
 
 
function setFooter($view)
 
{
  $this
->footer $view;
 
}
 
 
// display the template with header and footer
 
function view($view$data = array())
 
{
  $data 
$this->sanitize($data);
  
$this->load->view($this->header$data);
  
$this->load->view($view$data);
  
$this->load->view($this->footer$data);
 
}
 
 
// parse a template with header and footer
 
function parse($view$data = array())
 
{
  $data 
$this->sanitize($data);
  
$this->load->library('parser');
  return 
$this->parser->parse($this->header$dataTRUE);
  return 
$this->parser->parse($view$dataTRUE);
  return 
$this->parser->parse($this->footer$dataTRUE);
 
}
 
 
// sanitize data
 
function sanitize($data){
  
// title
  
if(!isset($data['title']))
  
{
   $data[
'title'"";
  
}
  
// ... (there are tonnes here but they are ecosystem specific)
  
return $data;
 
}

This allows me to display multiple views using the template class using basic simple, familiar syntax

$this->Template->setHeader('themes/default/header');
$this->Template->view('welcome_message'); 

You may notice that I edited the header and footer before using template to generate the content, this is due to the way the class works. Also this assumes the Template class is preloaded. It also works without the header and footer being set by using

$APPLICATION_PATH.'views/themes/default/' 

hope this helps someone else!

P.S. you could also have data specific to the header and footer passed by adding a variable $data = null and two internal properties of the Template class, headerData & footerData

 
Posted: 01 July 2012 10:51 AM   [ # 30 ]   [ Rating: 0 ]
Joined: 2012-07-01
1 posts

thanks a lot. got the same problem with loading of files =)

 
2 of 3
2