Class to load scripts and css in PHP [closed]

2

I need a method to load css and javascript files dynamically on each page, so as not to load unnecessary files and improve performance. Create a class in php that works, but I want to know if there is a better method. I do not use any PHP framework

/**
* Clase encargada de cargar los scripts
*/
class Autoload
{
    public static function loadJS($toLoad) {
        // Definir los scripts a cargar 
        $jquery = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js";
        // Classes javascript
        $logClass = "views/app/js/cls/Log.js";
        $userClass = "views/app/js/cls/User.js";
        $alertClass = "views/app/js/cls/Alert.js";
        // Funciones generales
        $generals = "views/app/js/generals.js";
        // Funciones de logeo y registro
        $loginScript = "views/app/js/login.js";
        $registerScript = "views/app/js/register.js";   
        // Bootstrap
        // Tether
        $libsTether = "https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js";
        $libsBootstrap = "views/libs/bootstrap/js/bootstrap.min.js";
        // Array de enlaces
        $src = Array(
            'jquery' => $jquery,
            'logClass' => $logClass,
            'userClass' => $userClass,
            'alertClass' => $alertClass,
            'generals' => $generals,
            'loginScript' => $loginScript,
            'registerScript' => $registerScript,
            'libsTether' => $libsTether,
            'libsBootstrap' => $libsBootstrap
            );
        /* Plantilla del array $toLoadJS obtenido
            $toLoad = Array(
                'jquery' => true,
                'logClass' => true,
                'userClass' => true,
                'alertClass' => true,
                'generals' => true,
                'loginScript' => true,
                'registerScript' => true,
                'libsTether' => true,
                'libsBootstrap' => true>
                );
        */      
        $html = '';
        foreach ($toLoad as $key => $value) {
            if ($value) {
                $script = ' <script src="' . $src[$key] . '"></script>' . "\n";                 $html = $html . $script;
            }
        }
        return $html;
    }
    public static function loadCSS($toLoad){
        // Definir las rutas de los estilos
        // Plantillas de paginas
        $hometemplate ='views/app/css/hometemplate.css';
        $registertemplate = 'views/app/css/registertemplate.css';
                    // Array de enlaces
        $href = Array(
            'hometemplate' => $hometemplate,
            'registertemplate' => $registertemplate
            );
        /* Plantilla del array $toLoadCSS obtenido
            $toLoad = Array(
                'hometemplate' => false,
                'registertemplate' => false
                );
        */
        $html = '';
        foreach ($toLoad as $key => $value) {
            if($value){
                $style = '<link href="' . $href[$key] . '" rel="stylesheet">' . "\n";
                $html = $html . $style;
            }
        }

        return $html;
    }
}

And in my individual pages I put this:

  $toLoadJS = Array(
   'jquery' => true,
   'logClass' => true,
   'userClass' => true,
   'alertClass' => true,
   'generals' => true,
   'loginScript' => true,
   'registerScript' => false,
   'libsTether' => false,
   'libsBootstrap' => true
  );
 $toLoadCSS = Array(
   'hometemplate' => true,
   'registertemplate' => false
   );
   echo Autoload::loadCSS($toLoadCSS);
   echo Autoload::loadJS($toLoadJS);
    
asked by Edwin V 12.02.2017 в 21:40
source

1 answer

-1

I understand that you want to create an external page or page where to save your CSS with your style, and / or, your PHP with your methods, and then load these in each of your individual pages.

If so, I invite you to review this link where they explain how to use the link tag. To call an external CSS sheet. link

    <link rel="stylesheet" href="">

Regarding PHP. See which of these two options are best suited to your needs.

include link

require_once link

This last link tries to explain the difference between the two. link

I hope this solves your doubts.

    
answered by 14.02.2017 в 13:41