/** * Adds external Javascript sources to the View. * * @example From within a PHP template: * <code> * $this->addJavascripts('/js/resource1.js', '/js/resource2.js'); * </code> * * @param string $src,... Unlimited list of external Javascript URLs * @return void */ public function addJavascripts() { // Get unique list of new javascripts to be added $view = $this->getJavascriptsView(); $args = func_get_args(); if (empty($args)) { return; } $toAdd = array_diff($args, $this->javascripts); $src = $this->javascriptsView->getSource(); $add = $this->getAssetVersion(); // Add scripts to the js loader if (($loader = $this->getJavascriptsLoader()) !== null) { if (empty($src)) { // TODO: Wat? if (substr($s, 0, 4) == 'http' || substr($s, 0, 2) == '//') { // Don't add variable to external scripts $src = "<script src=\"{$loader}?\" type=\"text/javascript\"></script>\n"; } else { $src = "<script src=\"{$loader}{$add}?\" type=\"text/javascript\"></script>\n"; } } foreach ($toAdd as $s) { if (substr($s, 0, 4) == 'http' || substr($s, 0, 2) == '//') { $src = "<script type=\"text/javascript\" src=\"{$s}\"></script>\n{$src}"; } else { $src = str_replace('" type=', 's[]=' . urlencode($s) . '?' . $add . '&" type=', $src); } } $this->javascriptsView->setSource($src); } else { // Add individual <script> tags for each source foreach ($toAdd as $s) { if (substr($s, 0, 4) == 'http' || substr($s, 0, 2) == '//') { // don't add variable to external scripts. $src .= "<script type=\"text/javascript\" src=\"{$s}\"></script>\n"; } else { $src .= "<script type=\"text/javascript\" src=\"{$s}{$add}\"></script>\n"; } } $this->javascriptsView->setSource($src); } }