Skip to content

TinyButStrong Template Engine

Derek Jones edited this page Jul 5, 2012 · 30 revisions

Category:Library::Template Engine | Category:Library::External

This is a template library that wraps around TinyButStrong.

Source

application/init/init_tbswrapper.php

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

if ( ! class_exists('Tbswrapper'))
{
     require_once(APPPATH.'libraries/tbswrapper'.EXT);
}

$obj =& get_instance();
$obj->tbswrapper = new Tbswrapper();
$obj->ci_is_loaded[] = 'tbswrapper';

?>

application/libraries/tbswrapper.php

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once("tbs_class_php5.php");

class Tbswrapper{

    /**
     * TinyButStrong instance
     *
     * @var object
     */
    private static $TBS = null;

    /**
     * default constructor
     *
     */
    public function __construct(){
        if(self::$TBS == null) $this->TBS = new clsTinyButStrong();
    }

    public function tbsLoadTemplate($File, $HtmlCharSet=''){
        return $this->TBS->LoadTemplate($File, $HtmlCharSet);
    }

    public function tbsMergeBlock($BlockName, $Source){
        return $this->TBS->MergeBlock($BlockName, $Source);
    }

    public function tbsMergeField($BaseName, $X){
        return $this->TBS->MergeField($BaseName, $X);
    }

    public function tbsRender(){
        $this->TBS->Show(TBS_NOTHING);
        return $this->TBS->Source;
    }

}

?>

Note: That was a php5 version of the wrapper. Here's the same thing in php4, still as the file application/libraries/tbswrapper.php:

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once("tbs_class_php4.php");

class Tbswrapper{

    /**
     * TinyButStrong instance
     *
     * @var object
     */
    var $TBS = null;

    /**
     * default constructor
     *
     */
    function Tbswrapper() {
        if($this->TBS == null) $this->TBS = new clsTinyButStrong();
    }

    function tbsLoadTemplate($File, $HtmlCharSet=''){
        return $this->TBS->LoadTemplate($File, $HtmlCharSet);
    }

    function tbsMergeBlock($BlockName, $Source){
        return $this->TBS->MergeBlock($BlockName, $Source);
    }

    function tbsMergeField($BaseName, $X){
        return $this->TBS->MergeField($BaseName, $X);
    }

    function tbsRender(){
        $this->TBS->Show(TBS_NOTHING);
        return $this->TBS->Source;
    }

}

?>

Besides tbswrapper.php, tbs_class_php5.php (for PHP5) or tbs_class_php4.php (for PHP4) must be present in application/libraries/ folder; also tbs_plugin_cache.php for caching presentation layer (it's different from CI caching system).

Usage

Like any CI library, you can either autoload it: configs/autoload.php

$autoload['libraries'] = array('tbswrapper');

Or, load it manually inside any controller method:

$this->load->library('tbswrapper');

Usage in controller

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Mycontroller extends Controller {

    function __construct()
    {
       parent::Controller();
       $this->load->library('tbswrapper');
    }

    function index()
    {

            $this->load->model('my_model');

            $data['appTitle'] = 'My APP';

            $data['userData'] = $this->my_model->getUserData();

            $this->load->view('my_view',$data);

    }
}

?>

Usage in view

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

function loadMyDefaultView($obj, $data){
    $obj->tbswrapper->tbsLoadTemplate(ROOTDIR.'templates/my_view.html');
    $obj->tbswrapper->tbsMergeField('vars', $data['vars']);
    $obj->tbswrapper->tbsMergeBlock('userDataBlock', $data['vars']['userData']);

    return $obj->tbswrapper->tbsRender();
}

echo  loadMyDefaultView($this, $data);

?>

Usage in model

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Functions that concern my model
 *
 */

class My_model extends Model {

    function __construct()
    {
        parent::Model();
    }

    /**
     * Get some information
     *
     * @return array
     */
    public function getUserData()
    {

        // of course you can launch a query here ...

        $ud[0]['user_realname'] = 'John DOE';
        $ud[0]['user_photo'] = 'picture1.png';
        $ud[1]['user_realname'] = 'John DOE Junior';
        $ud[1]['user_photo'] = 'picture2.png';

        return $ud;

    }

}

?>

Conclusion

Well, that's it for TinyButStrong. This template engine is really fast and unobtrusive. Give it a try ...

Clone this wiki locally