{"id":943,"date":"2013-05-12T01:21:39","date_gmt":"2013-05-12T09:21:39","guid":{"rendered":"http:\/\/blog.portnumber53.com\/?p=943"},"modified":"2013-05-12T01:21:39","modified_gmt":"2013-05-12T09:21:39","slug":"kohana-buffering-output-on-view-and-rendering-it-before-in-the-source-code","status":"publish","type":"post","link":"https:\/\/blog.portnumber53.com\/index.php\/2013\/05\/12\/kohana-buffering-output-on-view-and-rendering-it-before-in-the-source-code\/","title":{"rendered":"Kohana: Buffering output on view and rendering it before in the source code"},"content":{"rendered":"<p>Sometimes you need to hack the order the source code is going to be rendered in a view:<\/p>\n<p>&#8211; You want an extra piece of CSS that only aplies to that view, so you think it&#39;s not worth creating a single CSS file and including in your mashed file<\/p>\n<p>&#8211; You need the same as above for javascript<\/p>\n<p>&#8211; Your mind has not completely grasped the way to organize code in MVC&nbsp; \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Here&#39;s a way to do it:<\/p>\n<p>&nbsp;<\/p>\n<p>in your view you add something like this:<\/p>\n<pre>&lt;?php View::start_buffer(&#39;head_end&#39;); ?&gt;\n&lt;style&gt;\n    ::-moz-selection {\n        background: #b3d4fc;\n        text-shadow: none;\n    }\n\n&lt;\/style&gt;\n&lt;?php  View::end_buffer(&#39;head_end&#39;); ?&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>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&#39;m just adding the relevant changes for this to work; you can have your own extra sauce):<\/p>\n<p>&nbsp;<\/p>\n<pre>class View extends Kohana_View\n{\n    static public $buffers = array();\n\n    public static function start_buffer($buffer_name)\n    {\n        self::$buffers[$buffer_name] = &#39;&#39;;\n        ob_start();\n    }\n\n    public static function end_buffer($buffer_name)\n    {\n        self::$buffers[$buffer_name] = ob_get_clean();\n    }\n\n    public function render($file = NULL)\n    {\n        $rendered = parent::render($file);\n\n        if (! empty(self::$buffers['head_end']))\n        {\n            $rendered = str_replace(&#39;&lt;\/head&gt;&#39;, self::$buffers['head_end'] . &#39;&lt;\/head&gt;&#39;, $rendered);\n        }\n        return $rendered;\n    }\n\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>The buffer names are completely arbitrary, you can make them any way to want.<\/p>\n<p>I leave up to you to improve the way each buffer will be mapped to different locations in the source code.<\/p>\n<p>Drop me a line with your changes<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you need to hack the order the source code is going to be rendered in a view: &#8211; You want an extra piece of CSS that only aplies to that view, so you think it&#39;s not worth creating a single CSS file and including in your mashed file &#8211;&#8230; <a class=\"continue-reading-link\" href=\"https:\/\/blog.portnumber53.com\/index.php\/2013\/05\/12\/kohana-buffering-output-on-view-and-rendering-it-before-in-the-source-code\/\"> Continue reading <span class=\"meta-nav\">&rarr; <\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,13,18],"tags":[67,171,207,252,351],"class_list":["post-943","post","type-post","status-publish","format-standard","hentry","category-development","category-kohana","category-php","tag-buffer","tag-hack","tag-kohana","tag-mvc","tag-view"],"_links":{"self":[{"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/posts\/943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/comments?post=943"}],"version-history":[{"count":0,"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/posts\/943\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/media?parent=943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/categories?post=943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/tags?post=943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}