Welcome Guest, Not a member yet? Register   Sign In
Organizing Views (with Matchbox lib)
#1

[eluser]Armorfist[/eluser]
I'm building my CMS with CI and I'm trying to figure the best way to organize my views so that i dont repeat load->view every time i want to load the backend header for example. This is how I'm doing it:

For example, under the module "users" in the "application/modules/users/controllers/admin" directory, i have a controller named "admin" that has the following code:

Code:
<?php
class Admin extends Controller {

    function Admin ()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $user_check = $this->db->query('SELECT * FROM login_users');
        
                $teste = array(
        "teste1" => "value1",
        "teste2" => "value2",
        "db" => $user_check
        );
        
        $data['view_name'] = 'admin/teste';
        $data['view_module'] = 'users';
        $data['view_data'] = $teste;
        
        $this->load->view('backend/container',$data);
    }
}
?>

And under the folder "application/modules/users/views/admin" i have a view named teste.php with the following code:

Code:
Test

<?php
echo $teste1."<br/>";
echo $teste2."<br/>";

foreach ($db->result() as $row)
{
   echo $row->username."<br/>";
   echo $row->password."<br/>";
   echo $row->active."<br/>";
}
?&gt;

Now, under "application/views" i have a folder named "backend", which contains:

header.php - The header html code of the backend
menu.php - The menu of the backend
footer.php - The footer html code of the backend
login.php - The view that contains the login form

Then i have "cointainer.php", the view that loads the "header", "menu" and "footer" view along with the "content" view. The code:

Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
// If user isn't logued, include login view, else include backend layout views
if ($this->evdcms_login->is_logued)
{
    //Load Header
    $this->load->view('backend/header');
    
    //Load Menu
    $this->load->view('backend/menu');
    
    //Load Content based on $view_name,$view_module variable
    $this->load->module_view($view_module,$view_name,$view_data);
    
    //Load Content div end & Footer
    $this->load->view('backend/footer');
}
else
{
//Load login form    
$this->load->view('backend/login');
}
?&gt;

Am i doing this right? Suggestions will be greatly appreciated.
#2

[eluser]Armorfist[/eluser]
Any one? Sad
If i didn't explain myself well, please tell me.

Thanks
#3

[eluser]adamp1[/eluser]
You have done it exactly like me, but I don't like having to pass the module name over to it for when I want to load a module view.

Iv been racking my brains to try and find a cleaner simpler way but so far can't think of one.
#4

[eluser]Armorfist[/eluser]
Good point, i think one way of doing that is to get the URI segment correspondent to the module. Going to try that, at least its one less thing I have to specify in the controller.
#5

[eluser]adamp1[/eluser]
Problem is what if you have a controller with the same name as the module. You then don't pass the module name so that information won't be there?
#6

[eluser]Armorfist[/eluser]
I get your point, but i don't intend to use the "container.php" view for controllers that aren't inside modules, at least for the backend.




Theme © iAndrew 2016 - Forum software by © MyBB