CSS, JS Assets Manager - Menadżer plików CSS, JS na stronie HTML
Podany skrypt pokazuje jak w łatwy sposób zarządzać i includować pliki JS, CSS do naszej strony WWW. Dzięki temu jeśli mamy widget lub moduł który używa tej samej biblioteki JS/CSS, zostanie on tylko raz zaciągniety do strony.
Poniżej przykład użycia i kod źródłowy.
Listing
//Listing 1.0 - przykład użycia
require_once('Utils/Assets.php');
$manager = \Utils\Assets::getInstance();
//dodaj plik JS:
$manager->includeJS('/js/jquery.min.js');
//dodaj 2 pliki CSS
$manager->includeCSS('/css/jquery.css');
$manager->includeCSS('/css/styles.css');
//wyświetl zasoby na stronie:
echo $manager->render();
//zwróci:
<script src="/js/jquery.min.js" type="text/javascript"></script>
<link href="/css/jquery.css" rel="stylesheet" type="text/css" />
<link href="/css/styles.css" rel="stylesheet" type="text/css" />
//Listing 2.0 - Klasa Menadżera
namespace Utils;
/**
* Assets class to manage external resources like JS or CSS to be included only once in the page.
*
* @author Funkcje.net
*/
class Assets
{
private static $instance;
private $assets = array();
/**
* Private constructor to enfore singleton
*/
private function __construct()
{
$this->assets['js'] = array();
$this->assets['css'] = array();
}
/**
* Returns only once instance of a Assets Utils
*
* @return \Utils\Assets
* @static
*/
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Includes a JS file
*
* @param string $src file path to the JS file
*/
public function includeJs($src)
{
if (!isset($this->assets['js'][$src])) {
$this->assets['js'][$src] = $src;
}
}
/**
* Includes a CSS file
*
* @param string $src file path to the CSS file
*/
public function includeCss($src)
{
if (!isset($this->assets['css'][$src])) {
$this->assets['css'][$src] = $src;
}
}
/**
* Renders HTML output of JS and CSS tags
*
* @return string html output
*/
public function render()
{
$output = '';
foreach ($this->assets['js'] as $src) {
$output .= '<script src="'.$src.'" type="text/javascript"></script>';
}
foreach ($this->assets['css'] as $src) {
$output .= '<link href="'.$src.'" rel="stylesheet" type="text/css" />';
}
return $output;
}
}
require_once('Utils/Assets.php');
$manager = \Utils\Assets::getInstance();
//dodaj plik JS:
$manager->includeJS('/js/jquery.min.js');
//dodaj 2 pliki CSS
$manager->includeCSS('/css/jquery.css');
$manager->includeCSS('/css/styles.css');
//wyświetl zasoby na stronie:
echo $manager->render();
//zwróci:
<script src="/js/jquery.min.js" type="text/javascript"></script>
<link href="/css/jquery.css" rel="stylesheet" type="text/css" />
<link href="/css/styles.css" rel="stylesheet" type="text/css" />
//Listing 2.0 - Klasa Menadżera
namespace Utils;
/**
* Assets class to manage external resources like JS or CSS to be included only once in the page.
*
* @author Funkcje.net
*/
class Assets
{
private static $instance;
private $assets = array();
/**
* Private constructor to enfore singleton
*/
private function __construct()
{
$this->assets['js'] = array();
$this->assets['css'] = array();
}
/**
* Returns only once instance of a Assets Utils
*
* @return \Utils\Assets
* @static
*/
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Includes a JS file
*
* @param string $src file path to the JS file
*/
public function includeJs($src)
{
if (!isset($this->assets['js'][$src])) {
$this->assets['js'][$src] = $src;
}
}
/**
* Includes a CSS file
*
* @param string $src file path to the CSS file
*/
public function includeCss($src)
{
if (!isset($this->assets['css'][$src])) {
$this->assets['css'][$src] = $src;
}
}
/**
* Renders HTML output of JS and CSS tags
*
* @return string html output
*/
public function render()
{
$output = '';
foreach ($this->assets['js'] as $src) {
$output .= '<script src="'.$src.'" type="text/javascript"></script>';
}
foreach ($this->assets['css'] as $src) {
$output .= '<link href="'.$src.'" rel="stylesheet" type="text/css" />';
}
return $output;
}
}
Dodano przez: divix
Ranga: Administrator serwisu Punktów: 0
Ranga: Administrator serwisu Punktów: 0
Komentarze użytkowników
:: Losowe artykuły
:: Wymiana linków
Modowe inspiracje |
Android Gry i Aplikacje |
ZaplanujTransport.pl: Przeprowadzki, transport, aukcje |
Logo dla firmy |
Change Tires - Car Weather Forecast Reminder |
Laminas: MVC Framework for PHP |
IT Books Reviews and Programming: JS, JAVA, PHP, ANDROID, CSS |
Katalog roślin |
Programming articles: JAVA, PHP, C++, Python, JavaScript |
Kancelaria Adwokacka Łukasz Huszno