Question

I am just starting with CodeIgniter, but I can’t quite figure how to sort out my views.

I’ve made a sample layout explaining my problem, and is attached to this post.

I have a menu box, a user-system box and a content box.

In procedural PHP I would have a page called index.php with a parameter from GET, including the content. The user-system would just be included in the index.php file inside the box, and so would the menu.

How can I do this in a proper way using MVC and CodeIgniter?

enter image description here

Was it helpful?

Solution

Try Most Simple Template Library for CodeIgniter. It allows you to do what you would do in your php example. Create a "main" view and channel your content to it using your controller(s). You can create "subthemes" for body, content, sidebars etc.

OTHER TIPS

Hope you have configured codeigniter on your machine.

now create one file in controller folder for example : D:\wamp\www\demoProject\application\controllers\homePage.php

add following code in homePage controller

    <?php

    class HomePage extends CI_Controller {

        var $controller = "homePage";        
        var $viewContent = array();              

        function list_homePage() {   

// Load view pages // Load header view page

            $this->load->view('xome/header');

// Load main view page

            $this->load->view('xome/list_' . $this->controller, 
            $this->viewContent);

// Load footer view page

            $this->load->view('xome/footer');
        }
}
?>

after that create one file in view folder for example : D:\wamp\www\demoProject\application\views\list_homePage.php

put your html code in view file.

Hope this will help you... :)

For menu I would create a model like this:

class mMenu extends CI_Model{

 function mMenu(){
  parent::__construct();
 }

 function home(){
  $menu = array(
    'main_menu' => '<ul>
                      <li>Menu Link 1</li>
                      <li>Menu Link 2</li>
                      <li>Menu Link 3</li>
                    </ul>'
  );
  return $menu;
 }
}

In controller, say home:

function home(){
 $this->load->model('mMenu');
 $options['menu'] = $this->mMenu->home();

 $this->load->view('home_view', $options);
}

In the view file, wherever I need the menu to load:

echo $menu['main_menu'];

You should do something similar to control the state of the user and return the form if the user is not logged in and anything else if the user is logged in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top