Last active
October 9, 2017 07:16
-
-
Save lukaskleinschmidt/22db8fb3c92c60b23310cc187cae4432 to your computer and use it in GitHub Desktop.
Kirby JS Component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace LukasKleinschmidt\Component; | |
use HTML; | |
use STR; | |
use C; | |
/** | |
* Kirby Script Tag Component | |
* | |
* @package Kirby CMS | |
* @author Bastian Allgeier <[email protected]> | |
* @link http://getkirby.com | |
* @copyright Bastian Allgeier | |
* @license http://getkirby.com/license | |
*/ | |
class JS extends \Kirby\Component\JS { | |
/** | |
* Builds the html script tag for the given javascript file | |
* | |
* @param string $src | |
* @param boolean async | |
* @return string | |
*/ | |
public function tag($src, $async = false) { | |
if(is_array($src)) { | |
$js = array(); | |
foreach($src as $s) $js[] = $this->tag($s, $async); | |
return implode(PHP_EOL, $js) . PHP_EOL; | |
} | |
// auto template css files | |
if($src == '@auto') { | |
$filename = c::get('auto.js.filename', '{name}.js'); | |
$template = $this->kirby->site()->page()->template(); | |
$filename = str::template($filename, array('name' => $template)); | |
$filename = str::template($filename, array('modified' => f::modified($file))); | |
$file = str::template($filename, array('name' => $template)); | |
$root = $this->kirby->roots()->autojs() . DS . $file; | |
$src = $this->kirby->urls()->autojs() . '/' . $file; | |
if(!file_exists($root)) return false; | |
} | |
// build the array of HTML attributes | |
$attr = array('src' => url($src)); | |
if(is_array($async)) { | |
$attr = array_merge($attr, $async); | |
} else if($async === true) { | |
$attr['async'] = true; | |
} | |
return html::tag('script', '', $attr); | |
} | |
} | |
$kirby->set('component', 'js', __NAMESPACE__ . '\\JS'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment