CodeIgniter – Loading views within a view file

CodeIgniter will intelligently handle multiple calls to $this->load->view from within a controller.

If more then one call happens they will be appended together.
For example, you may wish to have a header view, a menu view, a content view, and a footer view. That might look something like this:

<?php

class Page extends Controller {

   function index()
   {
      $data['page_title'] = 'Your title';
      $this->load->view('header');
      $this->load->view('menu');
      $this->load->view('content', $data);
      $this->load->view('footer');
   }

}
?>

Another way is to fetch the content of the view and than assign it to the main view:

<?php

class Page extends Controller {

   function index()
   {
      $data['page_title'] = 'Your title';
      $data['menu'] = $this->load->view('menu');
      $this->load->view('content', $data);
   }

}
?>

The view:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?=$page_title?></title>
</head>
<body>
<div id="content">
  <div><?=$menu?></div>
</div>
</body>
</html>

Using a view helper:

If you have a very big and complex template you will see your controller becomes less readable.

By using this simple helper you can avoid to load everything into the controller by moving the template logic into the main view itself.

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if (!function_exists("load_view")){
	/**
	 * This helper can be used to load a view directly into a view file
	 * @param string $viewName
	 * @param array $data
         * @author Francesco Laurita
	 */
	function load_view($viewName,$data = array()){

		$CI = & get_instance();

		$content = $CI->load->view($viewName,$data,true);

		return $content;
	}
}
?>

Drop the content’s helper into a file called view_helper.php into the helper directory.
Load it as usual via autoload or load it at runtime with

$this->load->helper('view')

Now you can use it into your view:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?=$page_title?></title>
</head>
<body>
<div id="content">
<?php
 /**
  *
  *  Load the view file view/include/content.php
  *
  **/
?>
 <div><?=load_view('include/content',array('msg' => 'Hello!')) ?></div>
</div>
</body>
</html>

Hope this help 😉

Bookmark and Share

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

© 2013 Francesco Laurita' s blog Suffusion theme by Sayontan Sinha