Skip to content

TinyButStrong Template Engine

World Wide Web Server edited this page Jul 4, 2012 · 30 revisions

Category:Libraries Category:Libraries::Template Engines This is a template library that wraps around [url=http://www.tinybutstrong.com]TinyButStrong[/url].

[h3]FILES[/h3]

[b]application/init/init_tbswrapper.php[/b] [code] <?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';

?> [/code]

[b]application/libraries/tbswrapper.php[/b] [code] <?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;
}

}

?> [/code]

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

[h3]EXAMPLES[/h3]

Like any CI library, you can either autoload it:

[b]configs/autoload.php[/b]

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

Or, load it manually inside any controller method:

[code]$this->load->library('tbswrapper');[/code]

[h4]Example of usage :[/h4]

[b]controller[/b]

[code] <?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);

}

}

?> [/code]

[b]view[/b]

[code] <?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);

?> [/code]

[b]model[/b]

[code] <?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;

}

}

?> [/code]

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

Clone this wiki locally