Skip to content

Commit

Permalink
Fix for external js and css urls
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Sep 20, 2014
1 parent f2a266e commit f6d94d4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
6 changes: 4 additions & 2 deletions php/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
// 'template.css', // from jblank/css folder
'template.less', // from jblank/less folder
// 'template.scss',// from jblank/scss folder
// '//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css', // any external lib (you can use http:// or https:// urls)
))

// include JavaScript files
->js(array(
// 'libs/jquery-1.x.min.js',
// '//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', // any external lib (you can use http:// or https:// urls)
// 'libs/jquery-1.x.min.js', // your own local lib
'template.js',
))

Expand All @@ -52,7 +54,7 @@
))

// set custom generator
->generator('J!Blank Template') // null for disable
->generator('J!Blank.pro Joomla Template') // null for disable

// set HTML5 mode (for <head> tag)
->html5(true)
Expand Down
43 changes: 41 additions & 2 deletions php/libs/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,26 @@ public function css($filename, $type = 'all', $prefix = '')

} else if ($filename) {

$ext = pathinfo($filename, PATHINFO_EXTENSION);
$ext = $this->_getExtension($filename);
$prefix = (!empty($prefix) ? $prefix . '_' : '');

if ($ext == 'css') {

// include external
if ($this->_isExternal($filename)) {
$this->doc->addStylesheet($filename, 'text/css', $type);
return $this;
}

// include in css folder
$path = JPath::clean($this->cssFull . '/' . $prefix . $filename);
if ($mtime = $this->_checkFile($path)) {
$cssPath = $this->css . '/' . $prefix . $filename . '?' . $mtime;
$this->doc->addStylesheet($cssPath, 'text/css', $type);
return $this;
}

// include related root site path
$path = JPath::clean(JPATH_ROOT . '/' . $filename);
if ($mtime = $this->_checkFile($path)) {
$cssPath = rtrim($this->baseurl, '/') . '/' . ltrim($filename, '/') . '?' . $mtime;
Expand Down Expand Up @@ -258,7 +266,10 @@ public function js($filename, $prefix = '', $defer = false, $async = false)
$prefix = (!empty($prefix) ? $prefix . '_' : '');
$path = JPath::clean($this->jsFull . '/' . $prefix . $filename);

if ($mtime = $this->_checkFile($path)) {
if ($this->_isExternal($filename)) {
$this->doc->addScript($filename, "text/javascript", $defer, $async);

} else if ($mtime = $this->_checkFile($path)) {
$filePath = $this->js . '/' . $prefix . $filename . '?' . $mtime;
$this->doc->addScript($filePath, "text/javascript", $defer, $async);
}
Expand Down Expand Up @@ -440,6 +451,34 @@ protected function _getTemplatePath()
return rtrim($this->baseurl, '/') . '/templates/' . $path;
}

/**
* Check is path external
* @param string $path
* @return int
*/
protected function _isExternal($path)
{
$regs = array('http:\/\/', 'https:\/\/', '\/\/');
$reg = '#^(' . implode('|', $regs) . ')#iu';

return preg_match($reg, $path);
}

/**
* @param $filename
* @return mixed|string
*/
protected function _getExtension($filename)
{
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

if (strpos($ext, '?')) {
$ext = preg_replace('#(\?.*)$#', '', $ext);
}

return $ext;
}

/**
* Get absolute template path (filesystem)
* @return string
Expand Down

0 comments on commit f6d94d4

Please sign in to comment.