📜 ⬆️ ⬇️

Two Pass View in Kohana

In Kohana, I usually use the Template_Controller class. Very convenient - one layout, change only the content. But what if on one page we needed to include a CSS or JS file ?! C js-file is still fine, it can be connected in the middle of the page (but this is somehow ugly), but what about css ?! - this is invalid. Connect to the entire layout is also reluctant. I want to connect not in the controller, but in a template for example:
Copy Source | Copy HTML <?php head::addCSS( 'main' ) ?>
  1. Copy Source | Copy HTML <?php head::addCSS( 'main' ) ?>

and so that the file is correctly connected to the head of the document. There is nothing new here. In the symfony framework, this is already implemented from the first version. And it is natural that there is simply no other way how to handle html ready for release. The mechanism is very simple we take away completely ready document from Kohana and add the necessary lines to it. You can do this with the help of a hook, but I decided to do it with a controller override.
Add the head.php helper to the application / helpers folder
Copy Source | Copy HTML
  1. <? php
  2. / * <br/> * Helper accumulates the inserted css and js files. <br/> * And at the end inserts them into the layout. <br/> * /
  3. / ** <br/> * Insert style and javascript files <br/> * <br/> * @author Valera Sizov <br/> * /
  4. class head
  5. {
  6. static $ styleFiles = array ();
  7. static $ jsFiles = array ();
  8. static public function addingFiles ( $ layout )
  9. {
  10. $ css = array_merge (Kohana :: config ( 'style.files' ), self :: $ styleFiles );
  11. $ js = array_merge (Kohana :: config ( 'js.files' ), self :: $ jsFiles );
  12. $ out = '' ;
  13. foreach ( $ css as $ file )
  14. {
  15. $ out . = '<link rel = "stylesheet" type = "text / css" href = "/ static / css /' . $ file . '.css">' . "\ n" ;
  16. }
  17. foreach ( $ js as $ file )
  18. {
  19. $ out . = '<script src = "/ static / js /' . $ file . '.js" type = "text / javascript"> </ script>' . "\ n" ;
  20. }
  21. return str_replace ( '</ head>' , '$ out. "\ n". </ head>' , $ layout );
  22. }
  23. static public function addCss ( $ css )
  24. {
  25. if (is_array ( $ css )) {
  26. self :: $ styleFiles = array_merge (self :: $ styleFiles , $ css );
  27. } else {
  28. self :: $ styleFiles = array_merge (self :: $ styleFiles , array ( $ css ));
  29. }
  30. }
  31. static public function addJs ( $ js )
  32. {
  33. if (is_array ( $ js )) {
  34. self :: $ jsFiles = array_merge (self :: $ jsFiles , $ js );
  35. } else {
  36. self :: $ jsFiles = array_merge (self :: $ jsFiles , array ( $ js ));
  37. }
  38. }
  39. static public function removeCss ()
  40. {
  41. Kohana :: config_set ( 'style.files' , array ());
  42. }
  43. static public function removeJs ()
  44. {
  45. Kohana :: config_set ( 'js.files' , array ());
  46. }
  47. static public function addOnlyCss ( $ css )
  48. {
  49. self :: removeCss ();
  50. self :: addCss ( $ css );
  51. }
  52. static public function addOnlyJs ( $ js )
  53. {
  54. self :: removeJs ();
  55. self :: addJs ( $ js );
  56. }
  57. }
  58. ?>

Create an application / libraries / BaseController.php controller from which our controllers will inherit:
Copy Source | Copy HTML
  1. <? php
  2. abstract class BaseController extends Template_Controller {
  3. public $ template = 'frontend / layout' ;
  4. public function _render ()
  5. {
  6. if ( $ this -> auto_render == TRUE)
  7. {
  8. // Render the template when the class is destroyed
  9. echo head :: addingFiles ((string) $ this -> template);
  10. // $ this-> template-> render (TRUE);
  11. }
  12. }
  13. }
  14. ?>

And now we create two config application / config / style.php:
Copy Source | Copy HTML
  1. <? php
  2. / * Enumeration of javascript files that will be attached to the main template by default <br/> then you can override them or add them * /
  3. $ config [ 'files' ] = array ();
  4. ?>

And the second application / config / js.php:
Copy Source | Copy HTML
  1. <? php
  2. / * Enumeration of javascript files that will be attached to the main template by default <br/> then you can override them or add them * /
  3. $ config [ 'files' ] = array ();
  4. ?>

That's all. Of course, you can still arrange this all in the form of a module, so it will probably be more convenient to transfer it from project to project. But who needs to do it himself.
Now we write in the config default files that need to be connected. And then where necessary in the template we write:
Copy Source | Copy HTML
  1. <? php head :: addCss ( 'form_contacts' ) ?>

In the helper there is a whole set of methods.
I also plan to add a little of it in the line of checking for duplicates.
My blog

')

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


All Articles