Skip to content


Derek Jones edited this page · 11 revisions
Clone this wiki locally

TTemplate output library

The main purpose of this library is to automate the loading view process, so you don't have to call every time you want to load a view, in the controller $this->load->view(). A default view is calculated based upon routing informations, and is placed in $default_template variable. It first loads, the wrapper view, the view that contains the whole information of your site, and within the view you call <?php $tt->load_template($default_template) ?>. You can also load multiple views, just by calling this in any of the views, just as you would call $this->load->view() in controllers, but without passing data array.

Passing data to views

If you need to pass data to views, you place the data in $this->stash, in the controller, and the data would be available within any view.

Example: wrapper.php - wrapper file, in views folder

<div >&lt;?php $tt->load_template('menus/top') ?&gt;</div>
<div >&lt;?php $tt->load_template($default_template) ?&gt;</div>
<div >&lt;?php $tt->load_template('menus/bottom') ?&gt;</div>

menus/top.php in views folder

Here is my top menu | <a href="somewhere">Menulink</a>

menus/top.php in views folder

Here is my bottom menu | <a href="somewhereelse">BottomMenulink</a>

You must name the content view, upon the folder/controller/method, so in our example let's say we use action path the default controller, '/'.

You must have a view 'welcome/index.php', or setup in the controller the view yout want to load like this $this->stash['default_template'] = 'another/path';

In the first case you must have. Basicly, your view can contain everything, even load other templates.

The TTemplate library must be placed in the config/autoload.php, $autoload['libraries'] array.

&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 * TTemplate
 * Output automation library
 * @package        CodeIgniter
 * @author        Emil Dragu<>
 * @copyright    Copyright (c) 2009, Emil Dragu
 * @since        Version 1.0

 * TTemplate
 * This library enables automatic output, it loads automatically the view corresponding to
 * the action path. The path to the view is calculated from routing informations, 
 * the directory, controller and method that participate in the action. It also enables 
 * automatic content wrapping, just by adding in the wrapper template this 
 * &lt;?php $tt->load_template($default_template)?&gt;. Also in any template, you can include 
 * other templates by calling &lt;?php $tt->load_template('path/to/template') ?&gt;. Any data 
 * that must end up in templates(views) must be placed in controllers in $this->stash array.
 * The data will be available just as you would pass it to the view() function
 * $this->load->view('path/to/view',array())
 * Configuring this library
 * No configuration is basically needed, but you must know that it uses a default wrapper
 * template called 'wrapper', that must be placed on the root of views folder. This can be 
 * configured either by configuration file, a wrapper key must be placed
 * ($config['wrapper'] = 'custom_wrapper', or just temporarily, by placing a 'wrapper' key in 
 * the stash array($this->stash['wrapper'] = 'custom_wrapper'). Note that the wrapper is just 
 * an ordinary view where you can include other views by calling within it $tt->load_template('name').
 * The variable $default_template, contains the automatically view to be loaded, based upon 
 * routing informations, so you must call &lt;?php $tt->load_template($default_template)?&gt; in 
 * order to have the content actually wrapped. You can changed the value of the $default_template,
 * by creating a stash key called the same. So, if you do this in controller
 * ($this->stash['default_template'] = 'my/custom/template'), this is the template that is going to 
 * be wrapped. 
 * The name of this library is a tribute to perl's Template Toolkit(so that's where $tt comes from!) 
 * Hope they don't mind.
 * The notion 'template' can be considered equivalent to 'view'.
 * @package        CodeIgniter
 * @subpackage    Libraries
 * @category    Libraries
 * @author        Emil Dragu

class TTemplate
     * @access public
     * Configuration array
    public  $config;

     * @access private
     * The codeigniter instance

    private $_ci;

     * Starts buffering the output, sets internal variables

    public function __construct() {


        $this->_ci        =& get_instance();
        $this->_ci->stash = array();
        $this->config     = $this->_ci->config->config;

        $wrapper = 'wrapper';

        if (isset($this->config['wrapper']) && $this->config['wrapper'] != '') {

            $wrapper = $this->config['wrapper'];


        $this->_ci->stash['wrapper']          = $wrapper;
        $this->_ci->stash['tt']               = $this;
        $this->_ci->stash['default_template'] = $this->_default_template();


     * Calculates the default view to be wrapped
     * @return String Path to the default view to be wrapped

    private function _default_template() {

        $router = $this->_ci->router;

        return $router->directory . '/' . $router->class . '/' . $router->method;


     * Loads the view you name, just as you would load it with the 
     * $this->load->view() call.
     * @param String $template The template to be loaded

    public function load_template($template) {

       $this->_ci->load->view($template, $this->_ci->stash); 


     * This method starts the automated process of loading and wrapping views.
     * It is called first with the wrapper template as param, other template 
     * are loaded in the wrapper template.

    private function _process() {



     * This IGNITES the process. It gets the content of the output buffer, considering
     * any content as debug content, it cleans the buffer, print the debug output and start 
     * the process.

    public function __destruct() {

        $debug_info = ob_get_contents();


        //if you issue an echo anywhere outside the templates, the output should end up in the debug div

        if ($debug_info) {

            echo "<div style=\"border:1px solid red\"><h5 style=\"color: red\"> DEBUG OUTPUT</h5><pre style=\"font-size: 10px\">$debug_info</pre></div>";






Hope this is useful to you, any feedback would appreciated. Emil

Something went wrong with that request. Please try again.