I\'m fairly new to MVC, and I\'ve found CodeIgniter recently. I\'m still learning everyday, but one problem is its template engine. What is the best way to create templates
Well you can actually use a codeigniter library for templates. The most famous ones are:
Allow me propose an easier way to do this. Consider my answer to a similar question.
Pros:
Cons:
1. You may have to add a template
(or layout
—if you want to do it the Rails way) directory under views in order to structure your code properly. This follows from Pros[2].
2. Data for the specific view from the controller must first of all be passed to the template.
I've tried several ways to do codeigniter templates and the way that I stay is the fastest and simplest, is as follows.
In controller:
//Charge the view inside array
$data['body'] = $this->load->view('pages/contact', '', true);
//charge the view "contact" in the other view template
$this->load->view('template', $data);
In view template.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es">
<head>
<title>Template codeigniter</title>
</head>
<body>
<div>
<?=$body?>
</div>
<div class="clear"></div>
<div>Footer</div>
</div>
</body>
</html>
$body
is the view contact.
Make a library that includes all your views and send it data that you need to send to your content view. This is all!
<?php
class Display_lib
{
public function user_page($data,$name)
{
$CI =& get_instance ();
$CI->load->view('preheader_view',$data);
$CI->load->view('header_view');
$CI->load->view('top_navigation_view');
$CI->load->view($name.'_view',$data);
$CI->load->view('leftblock_view',$data);
$CI->load->view('rightblock_view',$data);
$CI->load->view('footer_view');
}
}
This library, easy to use and customize, does exactly what you'd expect:
Most Simple Template Library for CodeIgniter
I'm biased towards this template library made by Carmelo Capinpin because it is so easy to use: link text. Just copy the file in your library and you're ready to go. Instructions on how to use it is in the link I provided.