Sometimes you need to hack the order the source code is going to be rendered in a view:
– You want an extra piece of CSS that only aplies to that view, so you think it's not worth creating a single CSS file and including in your mashed file
– You need the same as above for javascript
– Your mind has not completely grasped the way to organize code in MVC 🙂
Here's a way to do it:
in your view you add something like this:
<?php View::start_buffer('head_end'); ?> <style> ::-moz-selection { background: #b3d4fc; text-shadow: none; } </style> <?php View::end_buffer('head_end'); ?>
Then, by taking advantage of the wonderful cascading file system that Kohana gives you, you create a View.php class in your application folder that will need to extend the render() function (I'm just adding the relevant changes for this to work; you can have your own extra sauce):
class View extends Kohana_View { static public $buffers = array(); public static function start_buffer($buffer_name) { self::$buffers[$buffer_name] = ''; ob_start(); } public static function end_buffer($buffer_name) { self::$buffers[$buffer_name] = ob_get_clean(); } public function render($file = NULL) { $rendered = parent::render($file); if (! empty(self::$buffers['head_end'])) { $rendered = str_replace('</head>', self::$buffers['head_end'] . '</head>', $rendered); } return $rendered; } }
The buffer names are completely arbitrary, you can make them any way to want.
I leave up to you to improve the way each buffer will be mapped to different locations in the source code.
Drop me a line with your changes