CodeIgniter/PHP - Calling a view from within a view

Posted by Jack W-H on Stack Overflow See other posts from Stack Overflow or by Jack W-H
Published on 2010-04-11T16:31:57Z Indexed on 2010/04/11 16:33 UTC
Read the original article Hit count: 661

Filed under:
|
|

Hi Folks

Basically for my webapp I'm trying to organise it a bit better. As it at the moment, every time I want to load a page, I have to do it from my controller like so:

        $this->load->view('subviews/template/headerview');
    $this->load->view('subviews/template/menuview');
    $this->load->view('The-View-I-Want-To-Load');
    $this->load->view('subviews/template/sidebar');
    $this->load->view('subviews/template/footerview'); 

As you can tell it's not really very efficient.

So I thought I'd create one 'master' view - It's called template.php. This is the contents of the template view:

<?php
    $view = $data['view'];

        $this->load->view('subviews/template/headerview');
        $this->load->view('subviews/template/menuview');
        $this->load->view($view);
        $this->load->view('subviews/template/sidebar');
        $this->load->view('subviews/template/footerview');
?>

And then I thought I'd be able to call it from a controller like this:

    $data['view'] = 'homecontent';
    $this->load->view('template',$data);

Unfortunately I simply cannot make this work. Does anyone have any ways around this or fixes I can put into place? I've tried putting ""s and ''s around $view in template.php but that makes no difference. The usual error is "Undefined variable: data" or "Cannot load view: $view.php" etc.

Thanks folks!

Jack

© Stack Overflow or respective owner

Related posts about php

Related posts about codeigniter