(My) View Classes - overkill?

This entry was posted by Colin at 12:12 on February 8, 2012 and is filed under Kohana.

I'm using the Kohana PHP framework at work, and realised the other day that I was going to have to set the page title in the controller, as there's no practical (and non-using-globals) way to get values from the content view, into the main layout template.

I looked into this, and came away with the recommendation that I should be using Mustache and the wonders of having OO views were a shiny new future. The way to use Mustache with Kohana is KOstache. Mustache is really cool, as it's really simple, and removes all logic from the view - there's no foreach, if, else, etc. I found this overkill though. I favour Smarty, and I like to be able to do (for instance) the following in the template:

{if $user}
   <a href="#">profile</a> | <a href="#">logout</a>
{else}
   <a href="#">login</a>
{/if}

This just isn't possible, by design, with mustache. You would have to handle this logic in the view class, which, if you wanted to keep markup out of code, you'd then need another two templates for the two forms of output.

This is possibly simplifying the view more than I can handle.

So what I've done is come up with my own view class - ViewO. This lets me do:

class ViewO_Admin_User_Index extends ViewO {	
   public $title = 'Users';
}

Or for something more dynamic:

class ViewO_Admin_User_Index extends ViewO {	
   public function title() {
      // do something funky
      return "My Dynamic Title";
   }
}

I quite like the way it's turned out, but it almost seems overkill. I don't think it is, but the thought's at the back of my mind.

Anyone want to weigh in with an opinion? Code (undocumented yet - but pretty simple) is on GitHub.

blog comments powered by Disqus