views:

103

answers:

1

I'm working on a project which allows the advanced user to define their own way of showing the information and access some basic API. For example, I provide a show_search_box() function so that the user can call this function in the view file when they want to show the standard search box, or they could call the function with parameters to customize the search form.

e.g. this code in the template will show a search form with watermark text "Enter keyword here".

<div><?php show_search_box('Enter keyword here'); ?></div>

What I'm thinking actually is exactly like what WordPress does in its template tags. (http://codex.wordpress.org/Stepping_Into_Template_Tags)

My idea is to create a class that provide all those API functions and pass an object instance of the class to the view file, so users can call the API functions in view like:

<div><?php $API->show_search_box('Enter keyword here'); ?></div>

I think it will work, (but have not tested it yet), but I prefer providing a set of direct called functions just like WordPress. What's the best way to do this with kohana 3?

======Update: I have tested the method of pass $API object to view, and it works as expected.

class API {
     public function show_search_box($watermark){....}
}

In the controller, pass the $API to the view/template

public function action_index()
{
     $this->template->API = new API();
}

Then call the function inside view/template as described above. Unlike those controller methods, $API cannot access the controller's variables unless they're explicitly assigned: e.g. $API->setVar('VarName', $a_controller_variable), which is quite tedious i think.

+1  A: 

Well, unlike Kohana 2.3, views don't execute in the controller namespace, so you can't simply do $this->something().

If you have all your functions in one Model, let's call it API, then you could do this in the view (or base controller if you want it available everywhere)...

$this->template->internalView = View::factory('your_view')
                                  ->set('API', Model::factory('API));

(assuming you have a <?php echo $internalView; ?> in a parent view).

Then you could do in your view...

<div><?php $API->show_search_box('Enter keyword here'); ?></div>

Which will run your method on your model. Views shouldn't really know about the existence of views, but your case may be an exception. Perhaps you could use a helper sort of class instead of a model, if you are worried about breaking the MVC paradigm.

If you want to do what WordPress does (have a bunch of global functions, which I don't recommend), then you will need to define them somewhere. Kohana doesn't really have an easy spot to place them, as it doesn't really cater for a bunch of global functions.

alex
+1 for helpers.
biakaveron
have tested the "helper class" method which works as expected, i think i will go with it. thanks for advice. using the global functions will make the template more intuitive: compare show_search_box(...) with $API->show_search_box(...), users without strong programming background will generally ask what "$API" is.
LazNiko