EllisLab text mark
Advanced Search
1 of 3
1
   
Loading views within a view file
Posted: 15 August 2008 06:22 PM   [ Ignore ]
Joined: 2008-07-09
6 posts

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?

 
Posted: 15 August 2008 07:13 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-06
918 posts

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

 
Posted: 15 August 2008 08:01 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2006-07-27
2617 posts

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

 
Posted: 15 August 2008 09:23 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2008-07-09
6 posts

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

 
Posted: 15 August 2008 09:44 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-06
918 posts

@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

 
Posted: 19 August 2008 03:30 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2007-12-04
27 posts

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

 
Posted: 23 December 2008 08:07 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2008-12-23
2 posts
prezet - 19 August 2008 07: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?

 
Posted: 24 December 2008 12:01 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2008-11-28
5 posts

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!

 
Posted: 21 August 2009 12:07 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2009-08-21
105 posts
sophistry - 16 August 2008 01: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?

 
Posted: 21 August 2009 01:00 PM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-06
918 posts

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

 
Posted: 01 February 2010 08:11 PM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2010-02-01
3 posts

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

 
Posted: 01 February 2010 10:27 PM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Joined: 2010-02-01
1 posts

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

 
Posted: 12 April 2010 06:55 PM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Joined: 2010-04-12
2 posts

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.

 
Posted: 13 April 2010 03:36 AM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Avatar
Joined: 2008-06-05
333 posts

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

 
Posted: 13 April 2010 04:09 AM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2007-09-01
820 posts
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.

 Signature 

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

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

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

 
1 of 3
1