Welcome Guest, Not a member yet? Register   Sign In
Build page function
#1

[eluser]Ollie Rattue[/eluser]
Hi there,

I find that I am always repeating the order in which I load my view files like this:

Code:
$data['title'] = 'Welcome to mysite - Admin Area - Events view';
$this->load->view('admin/header', $data);
$this->load->view('admin/top_message');    
$this->load->view('admin/left_panel');    
$this->load->view('admin/events/view', $data);
$this->load->view('admin/footer');

An disciple of the Don't Repeat Yourself rule, I was wondering how I would build a function to do this automatically. I am not sure whether a function can load files like this. I want to do something like this:

buildpage(TITLE."- View events", 'admin/events/view', $data);

It would condense my code so much, and make it easier if I want to add/remove an extra view file in the future.

Is this possible?
#2

[eluser]Jelmer[/eluser]
Just build a wrapper view that you load and pass any specific demands in variables which you process either directly into the view function or with switch statements. And load all the extra views from there.

Like this in your controller
(and putting al previously set options for the $data array in a key of the array and adding the page-type so you can pass the rest of the $data array into the views):
Code:
$data = array('data' => $data);
$data['page'] = 'events/view';
$this->load->view('admin/wrapper', $data);

And put this in your wrapper.php
Code:
$this->load->view('admin/header', $data);
$this->load->view('admin/top_message');    
$this->load->view('admin/left_panel');    
$this->load->view('admin/'.$page, $data);
$this->load->view('admin/footer');

I use this extensively as I use templates: the template's wrapper is loaded and checks if any specific views are available for the template (though in reality I made a view-wrapper function that does the checking as I didn't want to rewrite it for every partial view, it also checks if the default exists and produces a non-fatal error if not)
Code:
if (file_exists(APPPATH.'views/'.$template.'/'.$content_type.'.php'))
          $this->load->view($template.'/'.$content_type);
and if not loads the default template.
Code:
else $this->load->view('default/'.$content_type);




Theme © iAndrew 2016 - Forum software by © MyBB