views:

4884

answers:

5

I'm fairly new to MVC framework and found codeigniter recently. I'm still learning everyday but one problem is it's template engine. What is the best way to create it's template? CakePHP comes with it's own template library so how to the same with codeigniter?

+1  A: 

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.

Sample controller:

<?php
class Blog extends Controller {

    function index()
    {
     $data['title'] = "My Real Title";
     $data['heading'] = "My Real Heading";

     $this->load->view('blogview', $data);
    }
}
?>

Sample view:

<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
    <h1><?php echo $heading;?></h1>
</body>
</html>

Read more in the docs here: CodeIgniter User Guide: Views

pix0r
Well, yes. But i would to know if there's anyway to speed up this process and make it more organized - so i won't have to type in <header></header>, <body>, style etc... I want to have it in just one folder or library. Think of it as wordpress, you could change template without editing every single view.
Martin
+7  A: 

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:

Template Library

Simple Template Library

...more

Ryan Schumacher
+2  A: 

I use CodeIgniter with Smarty and it's great (if you like Smarty, I do).

Say you have an article controller, you could do somehting like this in it:

class Article extends Controller {
  function show_all() {
    $articles = $this->article_model->get_all();
    $this->smarty->assign('entities', $articles);
    $this->smarty->view('list');
  }
}

And then in your template:

{include file="header.tpl"}
  <ul>
  {foreach from=$entities item=entity}
  <li>{$entity.title}</li>
  {/foreach}
  </ul>
{include file="footer.tpl"}

The nice part about this is that the controller doesn't really need to know about headers and footers. It just knows that a group of articles should be shown as a list. From there, it's just the templates that are responsible for defining how a list of things are displayed, in this case, in a ul between a header and footer.

Another cool thing you can do is use this list template for things that aren't articles. You could have a list of users or pages or whatever. In some cases reusing a template like this can be useful. Not always, but sometimes.

Setting up CodeIgniter for smarty is pretty straightforward. It's a matter of copying the Smarty files to your library folder and creating a simple wrapper for it. You can find instructions here:

http://devcha.blogspot.com/2007/12/smarty-as-template-engine-in-code.html

Once you get is set up it's great.

GloryFish
A: 

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.

Randell
+1  A: 

This library, easy to use and customize, does exactly what you'd expect:

  • avoid HTML duplication (header, footer..)
  • no need to learn a new language (!)

Most Simple Template Library for CodeIgniter

Jerome Jaglale