KO3: how to deal with styles and script files

I am using Kohana 3 and a template controller. Currently, my main template template controller looks something like this:

<?php defined('SYSPATH') or die('No direct script access.');

abstract class Controller_SiteTemplate extends Controller_Template
{
    public function before()
    {
        parent::before();

        // Initialize default template variables
        $this->template->styles           = Kohana::config('site.styles');
        $this->template->scripts          = Kohana::config('site.scripts');

        $this->template->title            = '';
        $this->template->content          = '';
    }
}

And then in my template view I do:

<?php # Styles
foreach($styles as $file => $media) 
    echo HTML::style($file, array('media' => $media)).PHP_EOL ?>

<?php # Scripts
foreach($scripts as $file) 
    echo HTML::script($file).PHP_EOL ?>

It works well. The problem is that you do not have to worry about adding style files and a script to the controller. This also creates problems if the looks are done by someone other than me, as they will have to cheat the controller in order to add a new stylesheet or a new script file. How can this be done better?

, , . . , .

+3
4

Controller_Template javascripts, , Controller_Template.

/ , Controller_Template. , , , Kohana, , .

/, , , ?

+1

- , CSS Script . . , , .

:

<?php defined('SYSPATH') or die('No direct script access.');

class javascript {
    private static $files = array();

    public static function add($file)
    {
        self::$files[] = $file;
    }

    public static function get()
    {
        return self::$files;
    }
}

:

class Controller_Example extends Controller_Template {

    public function before()
    {
        ....
        javascript::add('js/jquery.js');
        javascript::add('js/jquery-ui.js');
        ....
    }
}

:

<html>
    <head>
        <?php foreach(javascript::get() as $javascript ?>
        <script type="text/javascript" src="<?= $javascript ?>"></script> 
        <?php endforeach; ?>
    </head>
...
</html>

:

  • CSS.
  • Script, , FBJS.

PS: , Kohana IRC.

0

? - , - , , , ..

:

<?php echo HTML::style("/css/style.css", array('media' => 'screen')).PHP_EOL ?>

0
    parent::before();

    // Initialize default template variables
    $this->template->styles           = Kohana::config('site.styles');
    $this->template->scripts          = Kohana::config('site.scripts');

    $this->template->title            = '';
    $this->template->content          = '';

    /* make your view template available to all your other views so easily you could
       access template variable
    */
    View::bind_global('template', $this->template);

-

   $template->scripts[] = 'js/example.js';

( ) js

  <?php
   foreach($template->scripts as $file) { echo Html::script($file), "\n"; }
  ?>

, ( script), , :

$top = View::factory('elements/top_nav')->render();
$content = $content->render();
$footer = View::factory('elements/footer')->render();

<div id="content"> 
        <?php echo $content;?>
    </div>
    <div id="meniu"><?php echo $top;?></div> you could add inline scripts as well, just add a $template->inline_scripts populate it with $template->inline_scripts = array('<script type="text/javascript">Cufon.replace(\'#meniu ul.topnav li a\');</script>'); and show it on template view script with a foreach($template->inline_scripts as $inline_script) { echo $inline_script."\n";};

`

0

Source: https://habr.com/ru/post/1745467/


All Articles