Actually adding many view in the controller is not very good, so I just found a very simple library
To use the library create the Template.php file in application/views/Template.php
and add the following code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Template {
var $template_data = array();
function set($name, $value)
{
$this->template_data[$name] = $value;
}
function load($template = '', $view = '' , $view_data = array(), $return = FALSE)
{
$this->CI =& get_instance();
$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));
return $this->CI->load->view($template, $this->template_data, $return);
}
}
Add to library application/config/autoload.php
the library to load with application
$autoload['libraries'] = array('template');
Create the template.php file (or the nomenclature you want) in the view folder and add the following code as test:
<html>
<body>
<div id="contents"><?= $contents ?></div>
</body>
</html>
The $ contents variable is where the view you want will be inserted.
As an example, create a view with the name about.php with the following code:
<h1>About</h1>
<p>I'm so human!</p>
Now to use the template add controller :
$this->template->load('template', 'about');
Where:
-
template : Name of the template file created in the view folder;
-
about : Name of the file that contains the content you want to insert into the $ contents variable.
If you want to add data that will be used in view , just add the array as the third parameter, as the example shows:
$this->template->load('template', 'about', array(‘titulo’=> “Titulo da pagina”));
I hope I have helped.