blog vipa

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

TinyButStrong template engine in Kohana PHP framework

styczeń 9, 2008 | 17:11

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:
  1. <?php defined(‘SYSPATH’) or die(‘No direct script access.’);
  2.  
  3. class Controller extends Controller_Core
  4. {
  5.         function __construct()
  6.         {
  7.                 parent::__construct();
  8.         }
  9.  
  10.         public function _kohana_load_view($template, $vars)
  11.         {
  12.                 if ($template == )
  13.                         return;
  14.  
  15.                 if (substr(strrchr($template, ‘.’), 1) === “html”)
  16.                 {
  17.                         $this->tbs->LoadTemplate($template);
  18.                         if (is_array($vars) && count($vars)> 0)
  19.                         {
  20.                                 foreach ($vars as $key => $val)
  21.                                 {
  22.                                         if(is_array($val))
  23.                                         {
  24.                                                 $this->tbs->MergeBlock($val[‘name’], $val[‘data’]);
  25.                                         }
  26.                                         else
  27.                                         {
  28.                                                 $this->tbs->MergeField($key, $val);
  29.                                         }
  30.                                 }
  31.                         }
  32.  
  33.                         $output = $this->tbs->Show(TBS_NOTHING);
  34.                 }
  35.                 else
  36.                 {
  37.                         $output = parent::_kohana_load_view($template, $vars);
  38.                 }
  39.  
  40.                 return $output;
  41.         }
  42. }

libraries/MY_View.php:

PHP:
  1. <?php
  2. defined(‘SYSPATH’) or die(‘No direct script access.’);
  3.  
  4. class View extends View_Core
  5. {
  6.         public function __construct($name, $data = NULL, $type = NULL)
  7.         {
  8.                 $name .= “.html” . $type;
  9.  
  10.                 if (!Kohana::find_file(‘views’, $name, TRUE, TRUE))
  11.                 {
  12.                         $type = Null;
  13.                 }
  14.  
  15.         parent::__construct($name, $data, $type);
  16.         }
  17. }

libraries/Tbs.php (the file is almost same like TBS_Wrapper for CodeIgniter):

PHP:
  1. <?php defined(‘SYSPATH’) or die(‘No direct script access.’);
  2.  
  3. require_once(“tbs_class_php5.php”);
  4.  
  5. class Tbs_Core
  6. {
  7.         private static $TBS = null;
  8.  
  9.         public function __construct()
  10.         {
  11.                 if(self::$TBS == null) $this->TBS = new clsTinyButStrong();
  12.         }
  13.  
  14.         public function LoadTemplate($File, $HtmlCharSet=‘UTF-8′)
  15.         {
  16.                 return $this->TBS->LoadTemplate($File, $HtmlCharSet);
  17.         }
  18.  
  19.         public function MergeBlock($BlockName, $Source)
  20.         {
  21.                 return $this->TBS->MergeBlock($BlockName, $Source);
  22.         }
  23.  
  24.         public function MergeField($BaseName, $X)
  25.         {
  26.                 return $this->TBS->MergeField($BaseName, $X);
  27.         }
  28.  
  29.         public function Show()
  30.         {
  31.                 $this->TBS->Show(TBS_NOTHING);
  32.                 return $this->TBS->Source;
  33.         }
  34. }







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:
  1. $config[‘autoload’] = array
  2. (
  3.         ‘libraries’ => ‘tbs, session, database’
  4. )

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:
  1. <?php defined(‘SYSPATH’) or die(‘No direct script access.’);
  2.  
  3. class Module_Controller extends Controller
  4. {
  5.         function index()
  6.         {
  7.                 $this->load->model(‘menu’);
  8.                 $menu = $this->menu->ListModules($dist_id, $arch_id);
  9.  
  10.                 $data = array (
  11.                         “title” => “Distribution’s modules list”,
  12.                         “menu_prefix” => “/dunno/yet/”,
  13.                         array( // will be processed with MergeBlock()
  14.                                 “name” => “modules”,
  15.                                 “data” => $menu
  16.                 ));
  17.  
  18.                 $view = $this->load->view(’sidebar/menu.html’, $data);
  19.                 $view->render(TRUE);
  20.         }
  21. }

views/sidebar/menu.html:

HTML:
  1. <ol><li><a href=“[menu_prefix]/show/[modules.key;block=li]“>[modules.val]</a></li></ol>



Happy MVC-ing :-)




Powiązane wpisy

1 Komentarz »

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

    Pingback od Learning Kohana » From the forums and the web, czerwiec 14, 2008 @ 17:55

Kanał RSS komentarzy TrackBack URI

Dodaj komentarz

Comments may need an approval.