{"id":89,"date":"2009-10-25T20:26:49","date_gmt":"2009-10-25T19:26:49","guid":{"rendered":"http:\/\/webpiraten.de\/?p=89"},"modified":"2009-10-28T20:04:50","modified_gmt":"2009-10-28T19:04:50","slug":"der-kohana-script-collector-helper","status":"publish","type":"post","link":"https:\/\/webpiraten.de\/index.php\/frameworks\/der-kohana-script-collector-helper\/","title":{"rendered":"der Kohana Script Collector Helper"},"content":{"rendered":"<p>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.<\/p>\n<p>Diese Scripte werden dann auf den jeweiligen Mastertemplates wieder an den richtigen Stellen eingebunden.<br \/>\nDazu habe ich einen neuen Helper unter application\/helpers\/collector.php eingerichtet.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\nclass Collector_Core\r\n{\r\n    \/**\r\n     * Arrays containing URL's to scripts\/styles (fill with standards)\r\n     * @var string\r\n     *\/\r\n    static protected $scripts       = array();\r\n    static protected $styles        = array();\r\n\r\n    \/**\r\n     * Adds a url to store\r\n     * @param string $file the local path to file\r\n     * @return void\r\n     *\/\r\n    static public function addJs($file)\r\n    {\r\n        self::$scripts&#x5B;] = $file;\r\n    }\r\n\r\n    \/**\r\n     * Adds a url to store\r\n     * @param string $file the local path to file\r\n     * @return void\r\n     *\/\r\n    static public function addCss($file)\r\n    {\r\n        self::$styles&#x5B;] = $file;\r\n    }\r\n\r\n    \/**\r\n     * Generates\/renders collectors items\r\n     * @param boolean      $print whether to echo the output or just return rendered string\r\n     * @return string      the rendered output\r\n     *\/\r\n    static public function renderJs($print = false)\r\n    {\r\n        $scripts    = array_unique(self::$scripts);\r\n        $output     = html::script($scripts);\r\n        if ($print)\r\n        {\r\n            echo $output;\r\n        }\r\n        else\r\n        {\r\n            return $output;\r\n        }\r\n    }\r\n\r\n    \/**\r\n     * Generates\/renders collectors items\r\n     * @param boolean      $print whether to echo the output or just return rendered string\r\n     * @param string|array $media type for this style (all, screen, print, media)\r\n     * @return string      the rendered output\r\n     *\/\r\n    static public function renderCss($print = false, $media = 'all')\r\n    {\r\n        $styles = array_unique(self::$styles);\r\n        $output = html::stylesheet($styles, $media);\r\n        if ($print)\r\n        {\r\n            echo $output;\r\n        }\r\n        else\r\n        {\r\n            return $output;\r\n        }\r\n    }\r\n} \/\/ end of Collector_Core\r\n\r\n<\/pre>\n<p>Dieser Helper kann nun aus allen Controllern heraus bef\u00fcllt werden.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\nclass Welcome_Controller extends Template_Controller\r\n{\r\n    \/**\r\n     * set master template\r\n     *\/\r\n    public $template = 'master_default.tpl';\r\n\r\n    \/**\r\n     * default constructor\r\n     * @param void\r\n     * @return void\r\n     *\/\r\n    public function __construct()\r\n    {\r\n        \/\/ load parent constructor\r\n        parent::__construct();\r\n\r\n        \/\/ collect scripts and styles\r\n        collector::addCss('\/css\/fancybox');\r\n        collector::addJs('\/js\/jquery.1.3.2');\r\n        collector::addJs('\/js\/jquery.fancybox');\r\n    }\r\n    \/** more code here *\/\r\n} \/\/ end of Welcome_Controller\r\n\r\n<\/pre>\n<p>Nachdem nun alle relevanten Skripte eingesammelt wurden, kann man diese auf dem Template wieder ausgeben lassen.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;?php collector::renderCss(true, 'all'); ?&gt;\r\n\r\n&lt;!-- html code here --&gt;\r\n\r\n&lt;?php collector::renderJs(true); ?&gt;\r\n\r\n<\/pre>\n<p>Der Kollektor sorgt daf\u00fcr das keine doppelten Skripte geladen werden.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,50,26,5],"tags":[69,16,71,72,641,9,633,68,70,73],"class_list":["post-89","post","type-post","status-publish","format-standard","hentry","category-frameworks","category-javascript","category-kohana-framework","category-php","tag-collector","tag-controller","tag-css","tag-helper","tag-javascript","tag-kohana","tag-php","tag-script","tag-stylesheet","tag-template"],"_links":{"self":[{"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/comments?post=89"}],"version-history":[{"count":18,"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":101,"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/posts\/89\/revisions\/101"}],"wp:attachment":[{"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}