blog vipa
mój dzień to 3 filiżanki kawy

blog vipa

TinyButStrong template engine in Kohana PHP framework

Styczeń 9th, 2008 . by vip

It 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:
[php]
tbs->LoadTemplate($template);
if (is_array($vars) && count($vars) > 0)
{
foreach ($vars as $key => $val)
{
if(is_array($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;
}
}
[/php]
libraries/MY_View.php:
[php]
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;
}
}
[/php]




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:
[php]
$config[‚autoload’] = array
(
‚libraries’ => ‚tbs, session, database’
)
[/php]
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:
[php]
load->model(‚menu’);
$menu = $this->menu->ListModules($dist_id, $arch_id);

$data = array (
„title” => „Distribution’s modules list”,
„menu_prefix” => „/dunno/yet/”,
array( // will be processed with MergeBlock()
„name” => „modules”,
„data” => $menu
));

$view = $this->load->view(‚sidebar/menu.html’, $data);
$view->render(TRUE);
}
}
[/php]
views/sidebar/menu.html:
[html]

  1. [modules.val]

[/html]

Happy MVC-ing :-)


One Response to “TinyButStrong template engine in Kohana PHP framework”

  1. Learning Kohana » From the forums and the web Says:

    […] Here you can find a tutorial on integrating the tinybutstrong template engine into Kohana. […]

Leave a Reply