user2458963
user2458963

Reputation: 23

Changing the behaviour of view in Codeigniter

I am using codeigniter for a project that is used by a variety of companies.

The default version of our software is up and running and works fine - however some of our customers want slightly different view files for their instance of the system.

Ideally what I would like to do is set a variable (for example VIEW_SUFFIX) and whenever a view file is loaded it would first check if there was a suffix version available if there was use that instead.

For example if the system had a standard view file called 'my_view.php' but one client had a VIEW_SUFFIX of 'client_1' - whenever I called $this->load->view('my_view') if the VIEW_SUFFIX was set it would first check if my_view_client_1 existed (and if it did use that) or if not use the default my_view.php.

I hope that my question is clear enough... If anyone has done this before or can think of a way to do it I would really appreciate it.

EDIT: Ideally I would like a solution that works without me changing every place that I am calling the view files. Firstly because there are a few files that may want different client versions and also because the view files are called from a lot of controllers

Upvotes: 2

Views: 176

Answers (2)

hw.
hw.

Reputation: 790

I had a similar requirement for which I created a helper function. Among other things, this function can check for a suffix before loading the specified view file. This function can check for the suffix and check if the file exists before loading it.

Unfortunately, the file checking logic would be a bit brittle. As an alternative, you can implement a MY_Loader class that will override the basic CI_Loader class.

Something like this in your application/core/MY_Loader.php:

class MY_Loader extends CI_Loader {
    protected function _ci_load($_ci_data)
    {
        // Copy paste code from CI with your modifications to check prefix.
    }
}

Upvotes: 1

Dale
Dale

Reputation: 10469

Could you not do this

// some method of creating $client
// probably created at login
$_SESSION['client'] = 'client_1';

$client = (isset($_SESSION['client'])) ? $_SESSION['client'] : '';
$this->load->view("your_view{$client}", $data);

Upvotes: 0

Related Questions