Part of the EllisLab Network
   
1 of 2
1
Loading views within a view file
Posted: 15 August 2008 05:22 PM   [ Ignore ]  
Summer Student
Total Posts:  6
Joined  07-09-2008

Hi,

I’m using multiple view files for various elements of the rendered page, such as the header, footer, and other widgets.

CodeIgniter docs suggest I should be loading these views entirely from the controller like so:

<?php

class User extends Controller {

    
public function register(){
        $this
->load->view('header');
        
$this->load->view('user/register');
        
$this->load->view('footer');
    
}

}
;

?> 

I have just recently begun loading the ‘header’ and ‘footer’ views from within the ‘user/register’ view itself using:

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

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?

Profile
 
 
Posted: 15 August 2008 06:13 PM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1101
Joined  08-06-2006

yes, but you have to use

$this->load->vars(array('var_name_you_want'=>'value')); 

somewhere (controller, generally) to get any variables defined ... or you could define them in the view (ick) and pass them as the second parameter.

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

Profile
 
 
Posted: 15 August 2008 07:01 PM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2774
Joined  07-27-2006

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:

class Blog extends Controller {
   
   
var $data = array();
   
   function 
Blog()
   
{
      parent
::Controller();
      
$this->data = array(
         
'header' => $this->load->view('header'''TRUE),
         
'footer' => $this->load->view('footer'''TRUE),
      );
   
}
   
   
function index()
   
{
      $data[
'blogs'$this->blog_model->get_recent(5);
      
$this->data['content'$this->load->view('blog/front'$dataTRUE);
      
$this->load->view('template'$this->data);
   
}
   

If you prefer abstraction on an application-wide scope, you can take a look at my Template library, linked in my sig below:

 Signature 

Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

Profile
 
 
Posted: 15 August 2008 08:23 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  6
Joined  07-09-2008

Two nice variations, thanks!

My end goal is to have CodeIgniter serve XML documents and use client side XSLT to render useful pages.

I’m hoping I can still achieve this using views, but I know hooking or rewriting the output class is another option.

Theres many great features in CI, just takes a bit of getting used to :D

Profile
 
 
Posted: 15 August 2008 08:44 PM   [ Ignore ]   [ # 4 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1101
Joined  08-06-2006

@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” wink

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! grin

class Blog extends Controller {
      
   
function Blog()
   
{
      parent
::Controller();
      
// load entire view output into object scope
      
$this->load->vars( array(
         
'header' => $this->load->view('header'''TRUE),
         
'footer' => $this->load->view('footer'''TRUE)
      ));
   
}
   
   
function index()
   
{
      $data[
'blogs'$this->blog_model->get_recent(5);
      
$data['content'$this->load->view('blog/front'$dataTRUE);
      
$this->load->view('template'$data);
   
}
   
 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

Profile
 
 
Posted: 19 August 2008 02:30 PM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  27
Joined  12-04-2007

How would you handle passing over a page title in this case? or if there a better way of handling page titles?

Profile
 
 
Posted: 23 December 2008 07:07 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  2
Joined  12-23-2008
prezet - 19 August 2008 06:30 PM

How would you handle passing over a page title in this case? or if there a better way of handling page titles?

Exactly what I was wondering about.. Can someone please answer this?

Profile
 
 
Posted: 23 December 2008 11:01 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  5
Joined  11-28-2008

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:

The views:

file: container_view.php

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

file: header_view.php

<html>
<
head>
<
title><?=$page_title;?></title>
</
head>
<
body

file: footer_view.php

</body>
</
html

file: some_content_view.php

<?$some_content?> 

The controller:

file: Test.php

<?php
class Test extends Controller{
    
    
function __construct()
    
{
        parent
::controller();
    
}
    
    
function index()
    
{
                
        
//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_datatrue);
        
        
//set other data that will be used in the container
        
$data['page_title''page title';
        
        
//show the container
        
$this->load->view('container'$data);
    
}
    
}
?> 

Cheers!

Profile
 
 
Posted: 21 August 2009 11:07 AM   [ Ignore ]   [ # 8 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  123
Joined  08-21-2009
sophistry - 16 August 2008 12:44 AM

@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” wink

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! grin

class Blog extends Controller {
      
   
function Blog()
   
{
      parent
::Controller();
      
// load entire view output into object scope
      
$this->load->vars( array(
         
'header' => $this->load->view('header'''TRUE),
         
'footer' => $this->load->view('footer'''TRUE)
      ));
   
}
   
   
function index()
   
{
      $data[
'blogs'$this->blog_model->get_recent(5);
      
$data['content'$this->load->view('blog/front'$dataTRUE);
      
$this->load->view('template'$data);
   
}
   

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?

Profile
 
 
Posted: 21 August 2009 12:00 PM   [ Ignore ]   [ # 9 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1101
Joined  08-06-2006

yes, the vars() method of the loader class allow a kind of “global” variable that is available to any view.

The Loader Class Documentation

 Signature 

peeker email (imap/pop) | site_migrate | OOCalendar | PhotoBox2 | word_limiter

Profile
 
 
Posted: 01 February 2010 07:11 PM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  5
Joined  02-01-2010

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

Profile
 
 
Posted: 01 February 2010 09:27 PM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  1
Joined  02-01-2010

Thanks for article ... I also studied about it .. your post or much ... thanks
—————————————-
du hoc uc

Profile
 
 
Posted: 12 April 2010 05:55 PM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  2
Joined  04-12-2010

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.

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

You can check parser library which i think is a good solution for your question.

 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:09 AM   [ Ignore ]   [ # 14 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  880
Joined  09-01-2007
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.

 Signature 

Kaydoo - A day in the life of a developer
BackendPro Control Panel

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

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

 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
 
 
   
1 of 2
1