5

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

2 Answers 2

13

I believe where you have:

$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');

You need to just get rid of the line:

$view = $data['view'];

This is because when the array is passed from the controller, the variable on the view can be accessed simply by $view rather than $data['view'].

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'll give it a go! I'll report back in a couple of minutes.
6

Quite a few suggestions here http://codeigniter.com/forums/viewthread/88335/

I chose this method: Controller class:

public function __construct() 
{
    parent::__construct();

    $this->load->vars(array(
        'header' => 'partials/header',
        'footer' => 'partials/footer',
    ));
}

public function index()
{       
    $data['page_title'] = 'Page specific title';        
    $this->load->view('my-view', $data);
}

View:

<?php $this->load->view($header, compact('page_title')); ?>
... blah blah ...
<?php $this->load->view($footer); ?>

Having to load the view in the view and pass through any variables that might be used by your child view is far from ideal. Being able to use something like Action Filters in Laravel would probably be better.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.