CodeIgniter views

Loading view in CodeIgniter

File 'application/controllers/page.php':

<?php
class Page extends CI_Controller {
	function index() {
		$this->load->view('template'); // includes file 'application/views/template.php'
		$this->load->view('folder_name/file_name'); // includes 'application/views/folder_name/file_name.php'
	}
}
?>

Now you can visit page: site.com/page/

Loading multiple views

If more than one view call happens they will be appended together.

<?php
class Page extends CI_Controller {
   function index() {
      $this->load->view('header');
      $this->load->view('menu');
      $this->load->view('content');
      $this->load->view('footer');
   }
}
?>

Passing dynamic data to the view

<?php
class Page extends CI_Controller {
   function index() {
      $data['title'] = 'Page title';
      $data['heading'] = 'Heading';
      $this->load->view('template', $data); // pass dynamic data to the view
   }
}
?>

File 'application/views/template.php':

<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
	<h1><?php echo $heading;?></h1>
</body>
</html>

Return view to a string

$view_template = $this->load->view('template', $data, TRUE); // return view

Leave a Reply

Your email address will not be published. Required fields are marked *