ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Loading views within a view file

August 15, 2008 6:22pm

Subscribe [25]
  • #1 / Aug 15, 2008 6:22pm

    wrs

    3 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');?>
    Yak yak yak
    <?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?

  • #2 / Aug 15, 2008 7:13pm

    sophistry's avatar

    sophistry

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

  • #3 / Aug 15, 2008 8:01pm

    Colin Williams's avatar

    Colin Williams

    2601 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', $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 / Aug 15, 2008 9:23pm

    wrs

    3 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

  • #5 / Aug 15, 2008 9:44pm

    sophistry's avatar

    sophistry

    906 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” 😉

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

    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 / Aug 19, 2008 3:30pm

    prezet

    14 posts

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

  • #7 / Dec 23, 2008 7:07pm

    TB

    2 posts

    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?

  • #8 / Dec 23, 2008 11:01pm

    cozzmyn

    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_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);
        }
        
    }
    ?>

    Cheers!

  • #9 / Aug 21, 2009 12:07pm

    kurucu's avatar

    kurucu

    99 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” 😉

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

    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);
       }
       
    }

    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 / Aug 21, 2009 1:00pm

    sophistry's avatar

    sophistry

    906 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

  • #11 / Feb 01, 2010 7:11pm

    skalrynd

    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

  • #12 / Feb 01, 2010 9:27pm

    Cronaldo

    1 posts

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

  • #13 / Apr 12, 2010 6:55pm

    jdavidson

    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.

  • #14 / Apr 13, 2010 3:36am

    Zeeshan Rasool's avatar

    Zeeshan Rasool

    261 posts

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

  • #15 / Apr 13, 2010 4:09am

    adamp1's avatar

    adamp1

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

    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.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases