Welcome Guest, Not a member yet? Register   Sign In
Having a single view for all controller methods?
#1

[eluser]Unknown[/eluser]
Hi

I'm new to CI.

I'm trying to find out if there's a way to render a single view for all methods of the same controller.

E.g. not having to call
Code:
$this->load->view()
for each and every method. I want the methods to only add / modify the existing view data.

Following is some pseudo code showing what I want to achieve. Please show me how can this be done in CI?

Code:
<?php

class My_class extends Controller {

    $data = array();

    function My_class() {
        parent::Controller();
        $this->data['title'] = NULL;
    }

    function _output($output) {
        // I want to use this same view for all methods
        $this->load->view('my_view', $this->data);
    }

    function index() {
        $this->data['title'] = 'Index';
    }

    function foo() {
        $this->data['title'] = 'Foo';
    }

    function bar() {
        $this->data['title'] = 'Bar';
    }

}
#2

[eluser]Cristian Gilè[/eluser]
Hi lists,

the quick and dirty way is to call the function _output from all methods:

Code:
$this->_output($output);

The best approach it to extend your controller and to use a template library. More info here and here

Cristian Gilè
#3

[eluser]TaylorOtwell[/eluser]
Or make a _remap() function that calls the intended controller function and then renders your view. You can find more info about the _remap function in the user guide.




Theme © iAndrew 2016 - Forum software by © MyBB