How do you manage styles and javascript’s files in Kohana? There is my solution…

When you create page in kohana you use several general css/js and more or less files specific only for several pages on site. It’s important include different files to different pages. So I realized this by creating abstract template class (in <site_root>/application/controllers/template.php). There is content of this file:

abstract class Template_Controller extends Controller {
	// Js files names
	private $_js_files = array();
	// Css files names
	private $_css_files = array();
	// Template view name
	public $template = 'template';
	// Default to do auto-rendering
	public $auto_render = TRUE;
	 * Template loading and setup routine.
	public function __construct()
		// Load the template
		$this-&gt;template = new View($this-&gt;template);
		if (TRUE == $this-&gt;auto_render)
			// Render the template immediately after the controller method
			Event::add('system.post_controller', array($this, '_render'));
	 * Render the loaded template.
	public function _render()
		if (TRUE == $this-&gt;auto_render)
			$this-&gt;input = new Input();
			// Js files
			$this-&gt;template-&gt;js_files = array_merge(Kohana::config('core.default_js_files', array()), $this-&gt;_js_files);
			// Css files
			$this-&gt;template-&gt;css_files = array_merge(Kohana::config('core.default_css_files', array()), $this-&gt;_css_files);
			// Render the template when the class is destroyed
	 * Add css files to head of document
	 * @param string @name
	public function add_css_file($name)
		$this-&gt;_css_files[] = $name;
	 * Add javascript files to head of document
	 * @param string @name
	public function add_js_file($name)
		$this-&gt;_js_files[] = $name;
} // End Template_Controller

There is we merge (by array_merge() ) js and css files from config for all pages and from controllers specified by `add_css_file()` and `add_js_file()`.

Well.. Now we can use arrays `js_files` and `css_files` in template of this class in <site_root>/application/view/template.php. There is no this template by default but feel free to create yourown template in this directory. In my case template looks like:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="" xml:lang="en" lang="en">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title><?php echo html::specialchars($title) ?></title>
	<?php foreach ($css_files as $css_file): ?>
	<link rel="stylesheet" href="<?php echo urlpath::css($css_file) ?>" type="text/css" media="screen" title="no title" charset="utf-8">
	<?php endforeach; ?>
	<?php foreach ($js_files as $js_file): ?>
	<script type="text/javascript" charset="utf-8" src="<?php echo urlpath::js($js_file) ?>" ></script>
	<?php endforeach; ?>
	<?php echo $content ?>

That’s it now you can use controllers extended from `Template_Controller` and specify files you need. Also you can notice that there is using my `urlpath` helper to create js/css links.

Do you have any another solution for this? Share one..

Books to read


  1. Как хорошо было Адаму: когда он произносил что-нибудь умное, он был уверен, что до него никто этого не говорил.
    What a good thing Adam had. When he said a good thing, he knew nobody had said it before. (С) Twain
    Надеюсь, Вы поняли к чему я об этом……

    • Yuri

      Я не стремлюсь изобретать, что то новое, просто рассказываю, как я это делаю, в надежде, что это будет кому нибудь интересно. Подскажите, если знаете, как реализовать это удобно в Zend Framework, буду искренне рад.

      I don’t want to invent something new I just tell about how I do that and hope that it’ll be interesting for someone. Maybe you can give advice how to implement something similar on Zend Framework – I will be sincerely glad.

  2. Pingback: TrackBack

Leave a Reply

Your email address will not be published. Required fields are marked *