Kohana 3.2.x: Master Template Controller [step 1]

Let's create a content module that will handle both static and dynamic content:

First enable the module:

modules/application/bootstrap.php

Kohana::modules(array(
.
.
'content' => MODPATH . 'content',   // Content module
.
.
));

 

Second, create the master template controller:

modules/content/classes/controller/base/template.php

template_name = Kohana::$config->load('content.template.name');
		$this->template = 'template/'.$this->template_name.'/index';
		parent::before();

		if ($this->auto_render) {
			$this->template->title		= '';
			$this->template->content	= '';

			$this->template->styles		= array();
			$this->template->scripts	= array();
		}

	}

	public function after() {
		if ($this->auto_render) {
			$styles = Kohana::$config->load('content.template.style');
			$scripts = Kohana::$config->load('content.template.script');

			$this->template->styles = array_merge( $this->template->styles, $styles );
			$this->template->scripts = array_merge( $this->template->scripts, $scripts );
		}
		parent::after();;
	}

}

 

Define routes to call the content module:

modules/content/init.php

<?php defined('SYSPATH') or die('No direct script access.');
 
/**
 * Routes for Content
 *
 * @package……Content
 * @category…..Routing
 * @author…….Mauricio Otta
 * @copyright….PortNumber53.com
 */
 
if (! Route::$cache) {
Route::set('homepage-content', '(<language–>"/)()',
array(
  'language' => '(pt-br)',
  'nothing' => '',
))
  ->defaults(array(
  'controller' => 'content',
  'action'     => 'homepage',
));

 Route::set('static-content', '(/).html',
  array(
   'language' => '(pt-br)',
   'path' => '[a-zA-Z0-9_/\.=\-]+',
  ))
  ->defaults(array(
   'controller' => 'content',
   'action'     => 'static',
  ));
}

 

Define the base content controller

response->body('Controller_Base_Content::index();
');
	}

	public function action_homepage() {
		$this->template->content = 'homepage';
	}

	public function action_static() {

		$path = $this->request->param('path');

		if ($filepath = Kohana::find_file('views', 'content/static/' . $path . '.html')) {
			$filepath = 'content/static/' . $path . '.html';
			//$this->template->content = 'static here'.$filepath;

			$content = new Model_Content();
			$latest_content = $content->get_last_posts(10);

			View::bind_global('latest_content', $latest_content);
			$this->template->content = View::factory($filepath, array(
				'latest_content' => $latest_content,
			))->render();

			switch ($path) {
				case 'privacy-policy': $title = 'Privacy Policy'; break;
				default:
					$title = 'unknown: ' . $path;
			}
			$this->template->facebook_og['og:title'] = $title;
			View::set_global('title', $title);
		} else {
			$this->action_dynamic();
		}
	}

	public function action_dynamic() {

		$path = '/'.$this->request->param('path', '');

		$content = new Model_Content();
		$latest_content = $content->get_last_posts(10);
		View::bind_global('latest_content', $latest_content);

		$post = $content->get_post_by_url($path);

		if ($post && $latest_content) {
			$this->template->content = View::factory('content/main', array(
					'latest_content' => $latest_content,
					'content' => $post,
				)
			);
		} else {
			$this->template->content = View::factory('content/main', array(
					'content' => array(
						'type' => Content::TYPE_UNKNOWN,
						'description' => 'Content not found',
					),
				)
			);
		}
	}
}

 

Define the content controller (~/modules/content/classes/controller/content.php):

<?php
class Controller_Content extends Controller_Base_Content { }
?>

 

Store some configuration values for the module (~/modules/content/config/content.php):

 array(
		'name' => 'default',
		'style' => array(
			'media/css/3-column-px.css' => 'screen',
			'media/css/screen.css' => 'screen, projection',
			'media/css/print.css' => 'print',
			'template/default/css/style.css' => 'screen',
		),
		'script' => array(
			'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js',
		),
	),
);

 

 

……………………………………………………………

 

Bookmark the permalink.

6 Responses to Kohana 3.2.x: Master Template Controller [step 1]

  1. tamahome14 says:

    I’m a little lost here.
    base content controller and parent controller has the same file name?

    • mauriciootta says:

      good catch
      I’ve update the post with my current implementation of this content module.
      there’s still more text to write as of now, but let me know if this works better.

  2. Ramaniks says:

     
    I'm not found this code in your article.
     
    modules/content/classes/controller/base/template.php
     
    <?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 {
    public function action_index() {
    $this—>template_name = Kohana::$config->load('content.template.name');
     
     
    In here, never not.
     
    modules/content/init.php
     

    <?php defined('SYSPATH') or die('No direct script access.');
     
    /**
     * Routes for Content
     *
     * @package……Content
     * @category…..Routing
     * @author…….Mauricio Otta
     * @copyright….PortNumber53.com
     */
     
    if (! Route::$cache) {
    Route::set('homepage-content', '(<language–>"/)()',
    array(
     

     

  3. Ramaniks says:

     
    Where are this written?
     
     

    Define the content controller
     
    Store some configuration values for the module:

     

    array(
    'name' => 'default',
    'style' => array(
    'media/css/3-column-px.css' => 'screen',
    'media/css/screen.css' => 'screen, projection',
    'media/css/print.css' => 'print',
    'template/default/css/style.css' => 'screen',
    ),
    'script' => array(
    'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js&#039;,
    ),
    ),
    );

     

Leave a Reply