Frontend\Core\Engine\Header::addJS PHP Method

addJS() public method

Add a javascript file into the array
public addJS ( string $file, boolean $minify = true, boolean $addTimestamp = null, integer $priorityGroup = self::PRIORITY_GROUP_DEFAULT )
$file string The path to the javascript-file that should be loaded.
$minify boolean Should the file be minified?
$addTimestamp boolean May we add a timestamp for caching purposes?
$priorityGroup integer
    public function addJS($file, $minify = true, $addTimestamp = null, $priorityGroup = self::PRIORITY_GROUP_DEFAULT)
    {
        $file = (string) $file;
        $minify = (bool) $minify;
        $addTimestamp = (bool) $addTimestamp;
        // get file path
        if (mb_substr($file, 0, 4) != 'http') {
            $file = Theme::getPath($file);
        }
        // no minifying when debugging
        if ($this->getContainer()->getParameter('kernel.debug')) {
            $minify = false;
        }
        if ($minify) {
            $file = $this->minifyJS($file);
        }
        $jsFile = array('file' => $file, 'add_timestamp' => $addTimestamp, 'priority_group' => $priorityGroup);
        // only add when not already in array
        if (!isset($this->jsFiles[$file])) {
            $this->jsFiles[$file] = $jsFile;
        }
    }

Usage Example

Example #1
0
 /**
  * Execute the action
  * If a javascript file with the name of the module or action exists it will be loaded.
  */
 public function execute()
 {
     // build path to the module
     $frontendModulePath = FRONTEND_MODULES_PATH . '/' . $this->getModule();
     // build URL to the module
     $frontendModuleURL = '/src/Frontend/Modules/' . $this->getModule() . '/Js';
     // add javascript file with same name as module (if the file exists)
     if (is_file($frontendModulePath . '/Js/' . $this->getModule() . '.js')) {
         $this->header->addJS($frontendModuleURL . '/' . $this->getModule() . '.js', true, true, Header::PRIORITY_GROUP_MODULE);
     }
     // add javascript file with same name as the action (if the file exists)
     if (is_file($frontendModulePath . '/Js/' . $this->getAction() . '.js')) {
         $this->header->addJS($frontendModuleURL . '/' . $this->getAction() . '.js', true, true, Header::PRIORITY_GROUP_MODULE);
     }
 }