TinyButStrong template engine in Kohana PHP framework
Styczeń 9th, 2008 . by vipIt took me a while to set up a TinuButStrong template engine into Kohana. I hope, this post will help You do it faster. You will need at least revision 1690 of Kohana (it didn’t worked for me with stable release). You can download it manually from their SVN/Trac or build rpm package from kohana.spec on DEVEL branch for PLD-Linux.
You will need to add some files to Your application/ dir.
libraries/MY_Controller.php:
-
-
class Controller extends Controller_Core
-
{
-
function __construct()
-
{
-
parent::__construct();
-
}
-
-
public function _kohana_load_view($template, $vars)
-
{
-
if ($template == ”)
-
return;
-
-
{
-
$this->tbs->LoadTemplate($template);
-
{
-
foreach ($vars as $key => $val)
-
{
-
{
-
$this->tbs->MergeBlock($val[‘name’], $val[‘data’]);
-
}
-
else
-
{
-
$this->tbs->MergeField($key, $val);
-
}
-
}
-
}
-
-
$output = $this->tbs->Show(TBS_NOTHING);
-
}
-
else
-
{
-
$output = parent::_kohana_load_view($template, $vars);
-
}
-
-
return $output;
-
}
-
}
libraries/MY_View.php:
libraries/Tbs.php (the file is almost same like TBS_Wrapper for CodeIgniter):
-
-
require_once(„tbs_class_php5.php”);
-
-
class Tbs_Core
-
{
-
-
public function __construct()
-
{
-
if(self::$TBS == null) $this->TBS = new clsTinyButStrong();
-
}
-
-
public function LoadTemplate($File, $HtmlCharSet=‘UTF-8′)
-
{
-
return $this->TBS->LoadTemplate($File, $HtmlCharSet);
-
}
-
-
public function MergeBlock($BlockName, $Source)
-
{
-
return $this->TBS->MergeBlock($BlockName, $Source);
-
}
-
-
public function MergeField($BaseName, $X)
-
{
-
return $this->TBS->MergeField($BaseName, $X);
-
}
-
-
public function Show()
-
{
-
$this->TBS->Show(TBS_NOTHING);
-
return $this->TBS->Source;
-
}
-
}
Additionally, upload the tbs_class_php5.php file into libraries/ too. You can also upload it to vendors/ dir and load it using Kohana loader classes.
You can autoload it in config/config.php by adding tbs into the autoload property:
-
(
-
‘libraries’ => ‘tbs, session, database’
-
)
If $data array would contain another array, it will be processed with MergeBlock(), otherwise it will be MergeFielded().
Now, You’re ready to use it with Your app.
controllers/module.php:
-
-
class Module_Controller extends Controller
-
{
-
function index()
-
{
-
$this->load->model(‘menu’);
-
$menu = $this->menu->ListModules($dist_id, $arch_id);
-
-
„title” => „Distribution’s modules list”,
-
„menu_prefix” => „/dunno/yet/”,
-
„name” => „modules”,
-
„data” => $menu
-
));
-
-
$view = $this->load->view(‘sidebar/menu.html’, $data);
-
$view->render(TRUE);
-
}
-
}
views/sidebar/menu.html:
Happy MVC-ing :-)


Czerwiec 14th, 2008 at 17:55
[...] Here you can find a tutorial on integrating the tinybutstrong template engine into Kohana. [...]