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

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

Code:
<?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:

Code:
<?php $this->load->view('header');?>
<p>Yak yak yak</p>
&lt;?php $this->load->view('footer');?&gt;

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?
#2

[eluser]sophistry[/eluser]
yes, but you have to use
Code:
$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.
#3

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

Code:
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', $data, TRUE);
      $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:
#4

[eluser]wrs[/eluser]
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 Big Grin
#5

[eluser]sophistry[/eluser]
@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" ;-)

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

Code:
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', $data, TRUE);
      $this->load->view('template', $data);
   }
  
}
#6

[eluser]prezet[/eluser]
How would you handle passing over a page title in this case? or if there a better way of handling page titles?
#7

[eluser]TB[/eluser]
[quote author="prezet" date="1219188638"]How would you handle passing over a page title in this case? or if there a better way of handling page titles?[/quote]
Exactly what I was wondering about.. Can someone please answer this?
#8

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

Code:
&lt;?php
$this->load->view('header');
echo $content;
$this->load->view('footer');
?&gt;

file: header_view.php

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?=$page_title;?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

file: footer_view.php

Code:
&lt;/body&gt;
&lt;/html&gt;

file: some_content_view.php

Code:
&lt;?= $some_content; ?&gt;

The controller:

file: Test.php

Code:
&lt;?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_data, true);
        
        //set other data that will be used in the container
        $data['page_title'] = 'page title';
        
        //show the container
        $this->load->view('container', $data);
    }
    
}
?&gt;

Cheers!
#9

[eluser]kurucu[/eluser]
[quote author="sophistry" date="1218865464"]@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" ;-)

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

Code:
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', $data, TRUE);
      $this->load->view('template', $data);
   }
  
}
[/quote]

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?
#10

[eluser]sophistry[/eluser]
yes, the vars() method of the loader class allow a kind of "global" variable that is available to any view.

The Loader Class Documentation




Theme © iAndrew 2016 - Forum software by © MyBB