[eluser]Unknown[/eluser]
Code:
<?php
class User extends Controller
{
function __construct()
{
parent::Controller();
}
public function login()
{
$data['title'] = build_title('Log In');
$this->user->login();
$this->load->view('section/global/html', $data);
$this->load->view('section/global/header', $data);
$this->load->view('section/global/top', $data);
$this->load->view('user/login', $data);
$this->load->view('section/global/footer', $data);
}
public function logout()
{
$data['title'] = build_title('Log Out');
$this->user->logout();
$this->load->view('section/global/html', $data);
$this->load->view('section/global/header', $data);
$this->load->view('section/global/top', $data);
$this->load->view('user/logout', $data);
$this->load->view('section/global/footer', $data);
}
public function signup()
{
$data['title'] = build_title('Sign Up');
$this->load->view('section/global/html', $data);
$this->load->view('section/global/header', $data);
$this->load->view('section/global/top', $data);
$this->load->view('user/logout', $data);
$this->load->view('section/global/footer', $data);
}
}
?>
Alright, so take a look at the code above. You can see that I have three functions inside of the controller class. The problem is that there is a lot of repetitive code inside of each function. For each, I have to define the variables in $data, then load each of the views individually. Each view represents a section of the final page that will be sent to the browser - the start HTML, header, top navigation, then the actual page content followed by a footer.
Is there a way to make the code less repetitive? I don't want to have to write the same code every time for each controller function I have. Most of the view loads ($this->load->view) are the same for every single function since every page will follow a similar format. Also is there a way to automatically load multiple views at once?
I was thinking of something like this:
Code:
function start($data)
{
$this->load->view('section/global/html', $data);
$this->load->view('section/global/header', $data);
$this->load->view('section/global/top', $data);
}
function end($data)
{
$this->load->view('section/global/footer', $data);
}
function data() { ? }
class User extends Controller
{
....
function whatever()
{
$data = data();
start($data); $this->load->view('user/page', $data); end($data);
}
}
I would appreciate any help. Thanks.