der Kohana Script Collector Helper
25. Oktober 2009
Kommentare deaktiviert für der Kohana Script Collector Helper By webpirat
Um modular und agil in Kohana zu entwickeln, wurde ein Skript Kollektor notwendig, der aus allen Controllern (Template- oder Standard-Controllern) Skripte (CSS, Javascript) sammeln kann.
Diese Scripte werden dann auf den jeweiligen Mastertemplates wieder an den richtigen Stellen eingebunden.
Dazu habe ich einen neuen Helper unter application/helpers/collector.php eingerichtet.
class Collector_Core { /** * Arrays containing URL's to scripts/styles (fill with standards) * @var string */ static protected $scripts = array(); static protected $styles = array(); /** * Adds a url to store * @param string $file the local path to file * @return void */ static public function addJs($file) { self::$scripts[] = $file; } /** * Adds a url to store * @param string $file the local path to file * @return void */ static public function addCss($file) { self::$styles[] = $file; } /** * Generates/renders collectors items * @param boolean $print whether to echo the output or just return rendered string * @return string the rendered output */ static public function renderJs($print = false) { $scripts = array_unique(self::$scripts); $output = html::script($scripts); if ($print) { echo $output; } else { return $output; } } /** * Generates/renders collectors items * @param boolean $print whether to echo the output or just return rendered string * @param string|array $media type for this style (all, screen, print, media) * @return string the rendered output */ static public function renderCss($print = false, $media = 'all') { $styles = array_unique(self::$styles); $output = html::stylesheet($styles, $media); if ($print) { echo $output; } else { return $output; } } } // end of Collector_Core
Dieser Helper kann nun aus allen Controllern heraus befüllt werden.
class Welcome_Controller extends Template_Controller { /** * set master template */ public $template = 'master_default.tpl'; /** * default constructor * @param void * @return void */ public function __construct() { // load parent constructor parent::__construct(); // collect scripts and styles collector::addCss('/css/fancybox'); collector::addJs('/js/jquery.1.3.2'); collector::addJs('/js/jquery.fancybox'); } /** more code here */ } // end of Welcome_Controller
Nachdem nun alle relevanten Skripte eingesammelt wurden, kann man diese auf dem Template wieder ausgeben lassen.
<?php collector::renderCss(true, 'all'); ?> <!-- html code here --> <?php collector::renderJs(true); ?>
Der Kollektor sorgt dafür das keine doppelten Skripte geladen werden.
collector, controller, css, helper, Javascript, kohana, PHP, script, stylesheet, template
Neueste Beiträge
Kategorien
- Agavi
- Android
- ANT
- Apache
- Bildbearbeitung
- CMS Systeme
- Codeigniter
- CSS
- Debugging
- Deployment
- Doctrine ORM
- Flash
- Frameworks
- GIT
- Javascript
- Kanban
- Kohana
- Layout & Styles
- Lightroom
- Lighttpd
- Linux
- Management & Prozesse
- MySQL
- Nginx
- OffTopic
- Photographie
- Photoshop
- PHP
- PhpMyAdmin
- PHPUnit
- Postfix
- Propel ORM
- Ruby On Rails
- Scrum
- Server
- Shell
- Sicherheit
- SOAP
- Social Media
- Sonata
- SVN
- Symfony2
- Versionierung
- VIM
- Wordpress
- XDebug
- XML
- XT Commerce
- Zend