Loading:


    CSS, JS Assets Manager - Menadżer plików CSS, JS na stronie HTML

    Podany skrypt pokazuje jak w łatwy sposób zarządzać i includować pliki JS, CSS do naszej strony WWW. Dzięki temu jeśli mamy widget lub moduł który używa tej samej biblioteki JS/CSS, zostanie on tylko raz zaciągniety do strony.

     

    Poniżej przykład użycia i kod źródłowy.



    Napisz Artyku³

    Listing

    //Listing 1.0 - przykład użycia

    require_once('Utils/Assets.php');
    $manager = \Utils\Assets::getInstance();

    //dodaj plik JS:
    $manager->includeJS('/js/jquery.min.js');
    //dodaj 2 pliki CSS
    $manager->includeCSS('/css/jquery.css');
    $manager->includeCSS('/css/styles.css');

    //wyświetl zasoby na stronie:
    echo $manager->render();
    //zwróci:
    <script src="/js/jquery.min.js" type="text/javascript"></script>
    <link href="/css/jquery.css" rel="stylesheet" type="text/css" />
    <link href="/css/styles.css" rel="stylesheet" type="text/css" />



    //Listing 2.0 - Klasa Menadżera

    namespace Utils;

    /**
     * Assets class to manage external resources like JS or CSS to be included only once in the page.
     *
     * @author Funkcje.net
     */

    class Assets
    {
        private static $instance;
        private $assets = array();

        /**
         * Private constructor to enfore singleton
         */

        private function __construct()
        {
            $this->assets['js'] = array();
            $this->assets['css'] = array();
        }

        /**
         * Returns only once instance of a Assets Utils
         *
         * @return \Utils\Assets
         * @static
         */

        public static function getInstance()
        {
            if (self::$instance === null) {
                self::$instance = new self();
            }
            return self::$instance;
        }

        /**
         * Includes a JS file
         *
         * @param string $src file path to the JS file
         */

        public function includeJs($src)
        {
            if (!isset($this->assets['js'][$src])) {
                $this->assets['js'][$src] = $src;
            }
        }

        /**
         * Includes a CSS file
         *
         * @param string $src file path to the CSS file
         */

        public function includeCss($src)
        {
            if (!isset($this->assets['css'][$src])) {
                $this->assets['css'][$src] = $src;
            }
        }

        /**
         * Renders HTML output of JS and CSS tags
         *
         * @return string html output
         */

        public function render()
        {
            $output = '';

            foreach ($this->assets['js'] as $src) {
                $output .= '<script src="'.$src.'" type="text/javascript"></script>';
            }

            foreach ($this->assets['css'] as $src) {
                $output .= '<link href="'.$src.'" rel="stylesheet" type="text/css" />';
            }

            return $output;
        }

    }
     




    Dodano przez: divix
    Ranga: Administrator serwisu Punktów: 0
    Komentarze użytkowników
      • Tre¶æ komentarza
        Kod do komentarza (opcjonalnie)
        PHP JavaScript MySQL Smarty SQL HTML CSS ActionScript
        Autor
        Token
        token

         

         








    funkcje.net
    Wszelkie prawa zastrzeżone©. | Funkcje.net 2008-2025 v.1.5 | design: diviXdesign & rainbowcolors