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() { parent::__construct(); // Load the template $this->template = new View($this->template); if (TRUE == $this->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->auto_render) { $this->input = new Input(); // Js files $this->template->js_files = array_merge(Kohana::config('core.default_js_files', array()), $this->_js_files); // Css files $this->template->css_files = array_merge(Kohana::config('core.default_css_files', array()), $this->_css_files); // Render the template when the class is destroyed $this->template->render(TRUE); } } /** * Add css files to head of document * @param string @name */ public function add_css_file($name) { $this->_css_files[] = $name; } /** * Add javascript files to head of document * @param string @name */ public function add_js_file($name) { $this->_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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <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; ?> </head> <body> <?php echo $content ?> </body> </html> |
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
|
Как хорошо было Адаму: когда он произносил что-нибудь умное, он был уверен, что до него никто этого не говорил.
What a good thing Adam had. When he said a good thing, he knew nobody had said it before. (С) Twain
Надеюсь, Вы поняли к чему я об этом……
Я не стремлюсь изобретать, что то новое, просто рассказываю, как я это делаю, в надежде, что это будет кому нибудь интересно. Подскажите, если знаете, как реализовать это удобно в 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.
Thanks.
Hey, I just hopped over to your site via StumbleUpon. Not somthing I would normally read, but I liked your thoughts none the less. Thanks for making something worth reading.
Very nice article. I’ve found your website on Yahoo, and I’ve been reading some useful information on it. Keep up the good work.
Thanks so much for writing all of the excellent information! Looking forward to checking out more posts!
I can see that you are an expert at your field! I am starting a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.
Thanks.
Pingback: TrackBack