Really add a lot view in controller is not very good, so I ended up finding a library very simple that I use.
To use the library create the file Template.php 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 file application/config/autoload.php
the library to load with application
$autoload['libraries'] = array('template');
Create the file php template. (or the nomenclature you want) in the folder view and add the following code as test:
<html>
<body>
<div id="contents"><?= $contents ?></div>
</body>
</html>
The variable $meet is where the view which you wish will be inserted.
As an example, I created a view with the name about php. with the following code:
<h1>About</h1>
<p>I'm so human!</p>
Now to use the add template on controller:
$this->template->load('template', 'about');
Where:
- template : Template file name created in folder view;
- about : Name of the file that contains the content you want to insert into the variable $meet.
If you want to add data that will be used in view, just add the array as the third parameter, as shown in the example:
$this->template->load('template', 'about', array(‘titulo’=> “Titulo da pagina”));
I hope I’ve helped.
All right, yes, I will edit the answer
– Fabricio Caceres