Using Smarty in Kohana

Oh god why would you use smarty in kohana!! I know it’s bad. My boss wants me to use smarty in kohana so I’m granting his wish. Still I don’t like doing it. Anyway while I’m at it why not use my time to write a article on how to use it.

A person with the nick d9 made a nice implementation of Kohana with Smarty using the normal template methods. So no writing smarty assigns in your controller wohoo!!

I uploaded it to my own server for bandwidth sake so here it is:

kohana2.3.3_smarty2.6.22

Well back to basics.  Lets first write the model that gets some values from the database. Since you all can guess how the database will look I’m not going to post a query script.

The model (user.php):


As you can see its the same as the standard models of kohana nothing different on it.

In the controller we will do 1 thing a bit different but other than that it will be the same (user.php):

read_users(); //smarty way $users = $user_model->read_users()->result(FALSE); $this->view = new View('user/index'); $this->view->users = $users; $this->template->content = $this->view; } }

I posted how I would do it normally and how I do it with smarty next to each other. As you can see a rule is added namely “result(FALSE)” because of this the result will be returned as a array in place of a object so that smarty can parse trough it. Also a comment on the Global_Controller I extend that is just a controller that contains my database connection and the global template namely “$template”.

Now as last lets show you the template namely (user/index.tpl) and see how smarty goes in its work:

{foreach from=$users key="k" item="report"} {/foreach}
ID Name Address City
{$report.id} {$report.name} {$report.address} {$report.city}

That is all you need to know. Easy isn’t it? Btw I still hate smarty implementations in kohana but it works.

EDIT:
Someone also released a smarty plugin for kohana3 look here: https://github.com/MrAnchovy/kohana-module-smarty/wiki

3 thoughts on “Using Smarty in Kohana”

  1. There are situations where one would want (or need) to use Smarty while also using Kohana. I don’t think it’s one of those “why would someone ever use X in Y” types of questions. Perhaps it’s not likely and perhaps it’s not desirable for most but the possibility is still there and real for some. Reasons…

    1.) You’re designers are used to Smarty and you do not want to waste the time needed for them to learn another tpl language (even PHP). Not all designers think like developers so for them, learning a new language is a hassle. Even if it’s just differences in syntax.
    2.) You have an existing site that uses Smarty and you’re porting it over to Kohana for some reason. Dunno how real this possibility is but if it can happen, expect it will.
    3.) Perhaps you don’t want your designers to be able to execute PHP code. Maybe this isn’t possible in Kohana but as far as I can tell, the views are straight up PHP.
    4.) You just plain feel like using Smarty :)

  2. KOstache is interesting. I’ll have to play with it. Generally, I don’t mind a little “display” logic in a tpl. Like if I want something to have a different css class based on some condition for example, I don’t mind that logic living right where it happens. Can’t really see a problem with it either (aside from maybe readability in extreme cases). Sure “display logic” and “display presentation” are two separate things just as “logic” and “presenation” are but I suppose for me, they’re just so closely related that they’re ok together.

Leave a Reply