Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Deal With Codeigniter Templates?

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 in CodeIgniter?

CakePHP comes with its own template library, is there a similar feature in CodeIgniter?

like image 551
Martin Avatar asked Jun 22 '09 23:06

Martin


People also ask

What is template in CodeIgniter?

A Codeigniter template is generally just a PHP file. You can use all the usual PHP syntax to output variables, do loops, and call other PHP code.

How to load view file in CodeIgniter?

To load (and display) a view in CodeIgniter, we use the built in Loader library. $this ->load->view( 'hello_world' , $data , true/false); This single line of code will tell CodeIgniter to look for hello_world. php in the application/views folder, and display the contents of the file in the browser.

Which one is the business logic in CodeIgniter?

The controller is the logic that connects our models with views. The controller forms the business logic of the application.


3 Answers

Unlike other frameworks CodeIgniter does not have a global template system. Each Controller controls it's own output independent of the system and views are FIFO unless otherwise specified.

For instance if we have a global header:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" > <html>     <head>         <title><?=$title?></title>         <!-- Javascript -->         <?=$javascript ?>         <!-- Stylesheets -->         <?=$css ?>     </head>     <body>         <div id="header">             <!-- Logos, menus, etc... -->         </div>         <div id="content"> 

and a global footer:

        </div>         <div id="footer">             <!-- Copyright, sitemap, links, etc... -->         </div>     </body> </html> 

then our controller would have to look like

<?php class Welcome extends Controller {      function index() {         $data['title'] = 'My title';         // Javascript, CSS, etc...          $this->load->view('header', $data);          $data = array();         // Content view data         $this->load->view('my_content_view', $data);          $data = array();         // Copyright, sitemap, links, etc...         $this->load->view('footer', $data);     } } 

There are other combinations, but better solutions exist through user libraries like:

See Comments Below

like image 182
Ryan Schumacher Avatar answered Sep 21 '22 21:09

Ryan Schumacher


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.

like image 22
jruzafa Avatar answered Sep 19 '22 21:09

jruzafa


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');        
    }
}
like image 38
SpartakusMd Avatar answered Sep 20 '22 21:09

SpartakusMd