{"id":500,"date":"2011-12-13T01:03:55","date_gmt":"2011-12-13T09:03:55","guid":{"rendered":"http:\/\/blog.portnumber53.com\/?p=500"},"modified":"2011-12-13T01:03:55","modified_gmt":"2011-12-13T09:03:55","slug":"kohana-3-2-x-master-template-controlle-step-1","status":"publish","type":"post","link":"https:\/\/blog.portnumber53.com\/index.php\/2011\/12\/13\/kohana-3-2-x-master-template-controlle-step-1\/","title":{"rendered":"Kohana 3.2.x: Master Template Controller [step 1]"},"content":{"rendered":"<p>Let&#39;s create a content module that will handle both static and dynamic content:<\/p>\n<p>First enable the module:<\/p>\n<p>modules\/application\/bootstrap.php<\/p>\n<pre>Kohana::modules(array(\n.\n.\n&#39;content&#39; =&gt; MODPATH . &#39;content&#39;,   \/\/ Content module\n.\n.\n));<\/pre>\n<p>&nbsp;<\/p>\n<p>Second, create the master template controller:<\/p>\n<p>modules\/content\/classes\/controller\/base\/template.php<\/p>\n<pre>template_name = Kohana::$config-&gt;load(&#39;content.template.name&#39;);\n\t\t$this-&gt;template = &#39;template\/&#39;.$this-&gt;template_name.&#39;\/index&#39;;\n\t\tparent::before();\n\n\t\tif ($this-&gt;auto_render) {\n\t\t\t$this-&gt;template-&gt;title\t\t= &#39;&#39;;\n\t\t\t$this-&gt;template-&gt;content\t= &#39;&#39;;\n\n\t\t\t$this-&gt;template-&gt;styles\t\t= array();\n\t\t\t$this-&gt;template-&gt;scripts\t= array();\n\t\t}\n\n\t}\n\n\tpublic function after() {\n\t\tif ($this-&gt;auto_render) {\n\t\t\t$styles = Kohana::$config-&gt;load(&#39;content.template.style&#39;);\n\t\t\t$scripts = Kohana::$config-&gt;load(&#39;content.template.script&#39;);\n\n\t\t\t$this-&gt;template-&gt;styles = array_merge( $this-&gt;template-&gt;styles, $styles );\n\t\t\t$this-&gt;template-&gt;scripts = array_merge( $this-&gt;template-&gt;scripts, $scripts );\n\t\t}\n\t\tparent::after();;\n\t}\n\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Define routes to call the content module:<\/p>\n<p>modules\/content\/init.php<\/p>\n<pre><!--?php defined('SYSPATH') or die('No direct script access.');\n\/**\n * Routes for Content\n *\n * @package......Content\n * @category.....Routing\n * @author.......Mauricio Otta\n * @copyright....PortNumber53.com\n *\/\n\nif (! Route::$cache) {\n\tRoute::set('homepage-content', '(<language-->&lt;?php defined(&#39;SYSPATH&#39;) or die(&#39;No direct script access.&#39;);\n \n\/**\n * Routes for Content\n *\n * @package&hellip;&hellip;Content\n * @category&hellip;..Routing\n * @author&hellip;&hellip;.Mauricio Otta\n * @copyright&hellip;.PortNumber53.com\n *\/\n \nif (! Route::$cache) {\nRoute::set(&#39;homepage-content&#39;, &#39;(&lt;language&ndash;&gt;&quot;\/)()&#39;,\narray(\n  &#39;language&#39; =&gt; &#39;(pt-br)&#39;,\n  &#39;nothing&#39; =&gt; &#39;&#39;,\n))\n  -&gt;defaults(array(\n  &#39;controller&#39; =&gt; &#39;content&#39;,\n  &#39;action&#39;     =&gt; &#39;homepage&#39;,\n));\n\n Route::set(&#39;static-content&#39;, &#39;(\/).html&#39;,\n  array(\n   &#39;language&#39; =&gt; &#39;(pt-br)&#39;,\n   &#39;path&#39; =&gt; &#39;[a-zA-Z0-9_\/\\.=\\-]+&#39;,\n  ))\n  -&gt;defaults(array(\n   &#39;controller&#39; =&gt; &#39;content&#39;,\n   &#39;action&#39;     =&gt; &#39;static&#39;,\n  ));\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Define the base content controller<\/p>\n<pre><!--?php defined('SYSPATH') or die('No direct script access.'); \/**  * Content  *  * @package......Content  * @category.....Base Controller  * @author.......Mauricio Otta  * @copyright....PortNumber53.com  * @license......Confidential. Internal use only.  *\/ class Controller_Base_Content extends Controller_Base_Template { \tpublic function action_index() { \t\t$this--->response-&gt;body(&#39;Controller_Base_Content::index();\n&#39;);\n\t}\n\n\tpublic function action_homepage() {\n\t\t$this-&gt;template-&gt;content = &#39;homepage&#39;;\n\t}\n\n\tpublic function action_static() {\n\n\t\t$path = $this-&gt;request-&gt;param(&#39;path&#39;);\n\n\t\tif ($filepath = Kohana::find_file(&#39;views&#39;, &#39;content\/static\/&#39; . $path . &#39;.html&#39;)) {\n\t\t\t$filepath = &#39;content\/static\/&#39; . $path . &#39;.html&#39;;\n\t\t\t\/\/$this-&gt;template-&gt;content = &#39;static here&#39;.$filepath;\n\n\t\t\t$content = new Model_Content();\n\t\t\t$latest_content = $content-&gt;get_last_posts(10);\n\n\t\t\tView::bind_global(&#39;latest_content&#39;, $latest_content);\n\t\t\t$this-&gt;template-&gt;content = View::factory($filepath, array(\n\t\t\t\t&#39;latest_content&#39; =&gt; $latest_content,\n\t\t\t))-&gt;render();\n\n\t\t\tswitch ($path) {\n\t\t\t\tcase &#39;privacy-policy&#39;: $title = &#39;Privacy Policy&#39;; break;\n\t\t\t\tdefault:\n\t\t\t\t\t$title = &#39;unknown: &#39; . $path;\n\t\t\t}\n\t\t\t$this-&gt;template-&gt;facebook_og['og:title'] = $title;\n\t\t\tView::set_global(&#39;title&#39;, $title);\n\t\t} else {\n\t\t\t$this-&gt;action_dynamic();\n\t\t}\n\t}\n\n\tpublic function action_dynamic() {\n\n\t\t$path = &#39;\/&#39;.$this-&gt;request-&gt;param(&#39;path&#39;, &#39;&#39;);\n\n\t\t$content = new Model_Content();\n\t\t$latest_content = $content-&gt;get_last_posts(10);\n\t\tView::bind_global(&#39;latest_content&#39;, $latest_content);\n\n\t\t$post = $content-&gt;get_post_by_url($path);\n\n\t\tif ($post &amp;&amp; $latest_content) {\n\t\t\t$this-&gt;template-&gt;content = View::factory(&#39;content\/main&#39;, array(\n\t\t\t\t\t&#39;latest_content&#39; =&gt; $latest_content,\n\t\t\t\t\t&#39;content&#39; =&gt; $post,\n\t\t\t\t)\n\t\t\t);\n\t\t} else {\n\t\t\t$this-&gt;template-&gt;content = View::factory(&#39;content\/main&#39;, array(\n\t\t\t\t\t&#39;content&#39; =&gt; array(\n\t\t\t\t\t\t&#39;type&#39; =&gt; Content::TYPE_UNKNOWN,\n\t\t\t\t\t\t&#39;description&#39; =&gt; &#39;Content not found&#39;,\n\t\t\t\t\t),\n\t\t\t\t)\n\t\t\t);\n\t\t}\n\t}\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Define the content controller (~\/modules\/content\/classes\/controller\/content.php):<\/p>\n<pre>&lt;?php\nclass Controller_Content extends Controller_Base_Content { }\n?&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>Store some configuration values for the module (~\/modules\/content\/config\/content.php):<\/p>\n<pre> array(\n\t\t&#39;name&#39; =&gt; &#39;default&#39;,\n\t\t&#39;style&#39; =&gt; array(\n\t\t\t&#39;media\/css\/3-column-px.css&#39; =&gt; &#39;screen&#39;,\n\t\t\t&#39;media\/css\/screen.css&#39; =&gt; &#39;screen, projection&#39;,\n\t\t\t&#39;media\/css\/print.css&#39; =&gt; &#39;print&#39;,\n\t\t\t&#39;template\/default\/css\/style.css&#39; =&gt; &#39;screen&#39;,\n\t\t),\n\t\t&#39;script&#39; =&gt; array(\n\t\t\t&#39;http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1\/jquery.js&#39;,\n\t\t),\n\t),\n);<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#39;s create a content module that will handle both static and dynamic content: First enable the module: modules\/application\/bootstrap.php Kohana::modules(array( . . &#39;content&#39; =&gt; MODPATH . &#39;content&#39;, \/\/ Content module . . )); &nbsp; Second, create the master template controller: modules\/content\/classes\/controller\/base\/template.php template_name = Kohana::$config-&gt;load(&#39;content.template.name&#39;); $this-&gt;template = &#39;template\/&#39;.$this-&gt;template_name.&#39;\/index&#39;; parent::before(); if ($this-&gt;auto_render) {&#8230; <a class=\"continue-reading-link\" href=\"https:\/\/blog.portnumber53.com\/index.php\/2011\/12\/13\/kohana-3-2-x-master-template-controlle-step-1\/\"> 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":[13,18],"tags":[],"class_list":["post-500","post","type-post","status-publish","format-standard","hentry","category-kohana","category-php"],"_links":{"self":[{"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/posts\/500","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=500"}],"version-history":[{"count":0,"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/posts\/500\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/media?parent=500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/categories?post=500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/tags?post=500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}