📜 ⬆️ ⬇️

Universal templating for Kohana 3.1

Hello, dear residents of Habr.

For quite a long time, I worked with various engines, frameworks, and other self-written variants of the PHP progress engine, as a result of which the brain, which was still young and eager for change, put down its own and, as is usually the case, very ambitious impression that everything should be done. Here, absolutely everything.

Delving into such monstrous pieces like Drupal or Joomla, I understood that yes, the abundance of a developed API and the presence of a large number of modules makes such engines indispensable for creating websites of almost any complexity, but moving to simpler things, like MVC CodeIgniter or Kohana frameworks (the latter essentially follows the concepts of HMVC), it was understood that it’s not always convenient to pound nuts with earth-to-earth missiles, and ease not only in handling the code, but also in the work of the site itself, which is called “solves”.
')
At the moment I am working on MVC (or HMVC, to be exact) Kohana framework, in particular, on the branches of versions 3.0 and 3.1, and, trying to find the most complete and elegant solution for an adequate template of the site, I was surprised to find that my Googling skills are morally obsolete, or the results are really summed up, but there is definitely no objective leader for my task.

Having collected some of my thoughts and ideas, I decided to combine them into a convenient and intuitive template system.

I say right away that I will not cite all the code, since it takes quite a few lines, plus, it includes not a description of not only the controller, but also some views, so I’ll tell you the general principle of the template engine with small examples.

At the core of templating is the controller template.php , which must be put in APPPATH.classes / controller /

The controlling controller itself is described as follows:
abstract class Controller_Template extends Controller

Therefore, in order for your controller to use the capabilities of templating, it must be inherited from the manager, for example:
class Controller_Welcome extends Controller_Template

General features template




In appearance, the number of functions is not particularly impressive, but I did not set myself the goal (at least for the first time) to create from this functionality at the level of phptemplate or something more cumbersome and comprehensive. The above functions are enough to determine the central system of templating the site, and no longer suffer, and this is the main task that stood before me.

Then I will give a brief description of the frauds in the controller.

Controller operation


In order for our controller, for which we enable templating, not to swear, it is necessary to specify the string parent :: before () in its function before to initialize templating.

During the initiation of the template engine and, accordingly, the automatic execution of its function before the following actions occur:
- Download template config
- Check and install the pager if the page argument is passed
- Creating an “empty” view and zeroing template variables

After these actions, the template is ready for operation and can be launched even without setting any parameters. The launch itself is carried out automatically, without calling any functions like render () or others, which saves us from having to follow the process of content generation.

However, our goal, on the contrary, was to provide us with the ability to easily control the template, right?

To modify the page, you can use the following constructions anywhere in the controller functions:

List of functions provided



Minor remarks

The above functions work in any order and act as storage devices for information that is compiled and rendered after the user action has been completed, so there is no need to follow the correct pattern generation sequence.

The functions of generating breadcrumbs, pager and content blocks and regions use my views stored in the template views folder by paths, respectively, breadcrumbs , pager and blocks .

Depending on the type of request by which an action is called, the template engine returns different content. For an HMVC request, this will only be the compiled and rendered content variable of the page, for the AJAX request it will be the same variable but wrapped in json_encode , and the usual request will render the entire page completely.

Total



Summing up, let me say once again that the template engine does not claim to be the best theme engine for Kohana, but is simply a convenient set of ideas for facilitating the management of the page's appearance. I note that there is room to grow, for example, add the ability to render in html or xhtml, screw the unloading mechanism for css-styles and js-scripts into the corresponding style and script tags to display them as a result as text, and not as an AJAX reference or HMVC-request, and many other tasty buns, but it will be done as needed.

Soon this branch will be uploaded to git so that anyone can easily download and update files as the functionality expands, but for the time being I apologize for the link to the file . Modifications and modifications are welcome.

I hope this template engine will help someone and get rid of unnecessary problems.

Thank you all for your attention, I will be glad to hear constructive criticism and suggestions!

Source: https://habr.com/ru/post/116237/


All Articles