Kohana-View_Model is a replacement for the default Kohana v3 class.
- Class based views
- Lighter controllers
- Takes logic out of your view templates
- Auto escaping of variable output
- Automatically escapes all view variables automatically
- Also lets you obtain "raw" data by prepending your variable with ^ ()
See the examples branch for examples and benchmarking.
This class is not backwards compatible with the stock view class. It completely replaces it.
Put your view class files in the classes/view/ directory and name them the same as your other classes.
// Note this is 'view' and not 'views'
// application/classes/view/foo/bar.php
class View_Foo_Bar extends View_Model
Create the associated view file (template) in your views directory with the same filename and path as your view class.
// application/views/foo/bar.php
Create your view template file the same as you would have before, but remember that any logic belongs in the view class. If you want to prevent the automatic variable escaping, prepend the variable with an '!'.
// application/views/foo/bar.php
<p>This view is <?=$adjective?>!</p>
<p>Another possible description is: <?=$random_adjective?></p>
<p>Here are my adjective notes: <?=^$adjective_note?></p>
Your view class will pass on properties and methods that begin with 'var_' to the template.
// application/classes/view/foo/bar.php
class View_Foo_Bar extends View_Model {
public $adjectives = array('awesome', 'neato', 'cool beans');
public $var_adjective = 'very nice';
public function var_random_adjective()
{
return array_rand($this->adjectives);
}
public function var_adjective_note()
{
$note = new Model_Adjective($this->var_adjective);
return $note->note;
}
}
And in your controller you could do
$bar = View_Model::factory('foo/bar')->set('var_adjective', 'the best');
echo $bar;
which would render:
This view is the best!
Another possible description is: cool beans
Here are my adjective notes: This is Super!!