Implementing components in Kohana
Imagine a situation that we have a widget on our site. This widget is repeated on the site on every page. How do we design the code to forget about this widget and focus on other issues?
Of course, we cannot afford to access the data area from the template, otherwise this would indicate our illiteracy. Many freyvers have their own solutions for this. But I will draw an analogy with symfony. In the latter there is such a thing as a component. It looks something like this: you call the static method in the template with the name of the components, then the controller of the same name is called, data is extracted from the data area and transferred to the template fragment, the latter is rendered and the code is inserted into the native template. Thus, the MVC architecture is embedded in each other. I simplified this scheme a little, I say right away, but I conveyed the essence for sure.
So here. There is no ready-made solution in Kohana. Therefore, I wrote a simple way to implement components that is solved in three lines of code.
So. We write widget.php helper and we throw it in the application / helpers folder. The helper content is as follows:
In the controller class, we write the usual content method only instead of rendering the template, we return it, i.e. it says return. I always use a slightly overridden Template_Controller controller. Example controller method:
We indicate the name of the template with underscore, so as not to be confused.
Now in any template it’s enough to write
And that's it - now instead of this line, our widget will be rendered. Just like that. If necessary, we can pass the necessary values to the component with the third argument in the form of an array.
By the way, partial (symfony like) are implemented by simple inclusion in the template
partial is a simple inclusion of the template (for those who don’t know)
PS Just let's not breed holivars about Smarty (etc.) vs Native Template.
There are many developments regarding Kohana - over time I will publish a series of articles on the blog here.
UPD I can’t transfer the blog post to Kohana. Not enough karma
Of course, we cannot afford to access the data area from the template, otherwise this would indicate our illiteracy. Many freyvers have their own solutions for this. But I will draw an analogy with symfony. In the latter there is such a thing as a component. It looks something like this: you call the static method in the template with the name of the components, then the controller of the same name is called, data is extracted from the data area and transferred to the template fragment, the latter is rendered and the code is inserted into the native template. Thus, the MVC architecture is embedded in each other. I simplified this scheme a little, I say right away, but I conveyed the essence for sure.
So here. There is no ready-made solution in Kohana. Therefore, I wrote a simple way to implement components that is solved in three lines of code.
So. We write widget.php helper and we throw it in the application / helpers folder. The helper content is as follows:
Copy Source | Copy HTML- class widget
- {
- public static function paste($className,$functionName,$parameters = null)
- {
- $controllerName = $className.'_Controller';
- $controller = new $controllerName;
- $controller->$functionName($parameters)->render(true);
- }
- }
- ?>
In the controller class, we write the usual content method only instead of rendering the template, we return it, i.e. it says return. I always use a slightly overridden Template_Controller controller. Example controller method:
Copy Source | Copy HTML- public function widget_example()
- {
- return View::factory('_example.php',array('var' => 'Привет мир!'));
- }
We indicate the name of the template with underscore, so as not to be confused.
Now in any template it’s enough to write
Copy Source | Copy HTML
And that's it - now instead of this line, our widget will be rendered. Just like that. If necessary, we can pass the necessary values to the component with the third argument in the form of an array.
By the way, partial (symfony like) are implemented by simple inclusion in the template
Copy Source | Copy HTML- render() ?>
partial is a simple inclusion of the template (for those who don’t know)
PS Just let's not breed holivars about Smarty (etc.) vs Native Template.
There are many developments regarding Kohana - over time I will publish a series of articles on the blog here.
UPD I can’t transfer the blog post to Kohana. Not enough karma