How to create and load view in codigniter?

  • In codigniter, views will never call directly. They must be loaded by the controller. In an MVC framework, the controller is responsible for loading a particular view.
  • Before going to view section, you must read about controllers in codigniter.

Page Contents:

  1. Creating a view
  2. Loading a view
  3. Loading multiple views
  4. Adding dynamic data to the view
  5. Storing views within subdirectories
  6. Creating loops
  7. Returning views as data

Creating a view

  • Create a view file test.php and store it in the directory applications/views/ directory.
                 <title>Welcome to </title>
                 <h1>Welcome to my Blog!</h1>

Loading a view

  • The view will load from the controller.
  • Suppose if you need to load test.php in the applications/views/ directory, you need to call like as follows.
  • If you visit the site URL, you can see the result.
        class Test extends CI_Controller {
        public function index()

Loading multiple views

  • For example, If you have a header view, a menu view, content view, and footer view. It will be loaded like this
        class Page extends CI_Controller {
                public function index()
                        $data['page_title'] = 'Your title';
                        $this->load->view('content', $data);

Storing views within subdirectories

  • If you prefer the type of organization, you can store the view files within the subdirectories.

Adding Dynamic Data to the View

    	$data = array(
		        'title' => 'My Title',
		        'heading' => 'My Heading',
		        'message' => 'My Message'

		$this->load->view('blogview', $data);