diff --git a/src/I18n.php b/src/I18n.php index d02ad04..a81e3de 100644 --- a/src/I18n.php +++ b/src/I18n.php @@ -128,8 +128,8 @@ class I18n { && Tpl :: initialized() ) { Tpl :: register_static_directory(self :: $root_path, null, 'locales/'); - Tpl :: add_js_file("lib/babel.js", "js/translation.js"); - Tpl :: add_js_file("locales/", "$locale.js"); + Tpl :: add_js_file("lib/babel.js", "js/translation.js", 10); + Tpl :: add_js_file("locales/", "$locale.js", 11); } if (php_sapi_name() == 'cli') { diff --git a/src/Tpl.php b/src/Tpl.php index dc5e493..a471fd9 100644 --- a/src/Tpl.php +++ b/src/Tpl.php @@ -31,7 +31,7 @@ class Tpl { * Smarty templates directories path with their priority * @var array */ - public static $templates_directories = array(); + public static $templates_directories = []; /** * Enable/disable AJAX returned data debugging in logs @@ -49,19 +49,19 @@ class Tpl { * Static directories path with their priority * @var array */ - private static $static_directories = array(); + private static $static_directories = []; /** * CSS files to load in next displayed page * @var array */ - private static $css_files = array(); + private static $css_files = []; /** * JavaScript files to load in next displayed page * @var array */ - private static $js_files = array(); + private static $js_files = []; /** * MIME type detector object @@ -104,9 +104,9 @@ class Tpl { 'cache_directory' => null, 'main_pagetitle' => null, 'static_root_url' => 'static/', - 'static_directories' => array(), - 'included_css_files' => array(), - 'included_js_files' => array(), + 'static_directories' => [], + 'included_css_files' => ["css/app.css"], + 'included_js_files' => ["js/app.js"], 'webstats_js_code' => null, 'upload_max_filesize' => null, 'debug_ajax' => App::get('debug_ajax', false, 'bool'), @@ -218,9 +218,9 @@ class Tpl { // Initialize errors & messages session variables if (!isset($_SESSION['errors'])) - $_SESSION['errors'] = array(); + $_SESSION['errors'] = []; if (!isset($_SESSION['messages'])) - $_SESSION['messages'] = array(); + $_SESSION['messages'] = []; } /** @@ -404,7 +404,7 @@ class Tpl { public static function get_errors() { if(isset($_SESSION['errors']) && is_array($_SESSION['errors'])) return $_SESSION['errors']; - return array(); + return []; } /** @@ -414,7 +414,7 @@ class Tpl { public static function get_messages() { if(isset($_SESSION['messages']) && is_array($_SESSION['messages'])) return $_SESSION['messages']; - return array(); + return []; } /** @@ -462,7 +462,11 @@ class Tpl { /** * Register JS file(s) to load on next displayed page - * @param string|array $args JS files to load + * + * Notes: + * - if first argument matched with one declared static directories, used it as static root URL + * - if last argument is an integer, used it as JS files inclusion priority + * @param string|int|array $args JS files to load * @return void */ public static function add_js_file(...$args) { @@ -475,13 +479,23 @@ class Tpl { ) ) $root_url = self :: clean_static_root_url(array_shift($args)); - foreach ($args as $files) { - if (!is_array($files)) $files = array($files); - foreach ($files as $file) { - $path = $root_url.$file; - if (!in_array($path, self :: $js_files)) - self :: $js_files[] = $path; - } + $base_priority = ( + count($args) > 1 && is_int($args[count($args)-1])? + intval(array_pop($args)): + ceil(max(max(self :: $js_files), 1000))+1 + ); + $files = call_user_func_array( + "array_merge", + array_map('\EesyPHP\ensure_is_array', $args) + ); + foreach ($files as $idx => $file) { + $path = $root_url.$file; + $file_priority = $base_priority + 0.001 * $idx; + self :: $js_files[$path] = ( + array_key_exists($path, self :: $js_files)? + min(self :: $js_files[$path], $file_priority): + $file_priority + ); } } @@ -510,7 +524,7 @@ class Tpl { // Handle CSS & JS files included self :: add_css_file(App::get('templates.included_css_files', null, 'array')); - self :: add_js_file(App::get('templates.included_js_files', null, 'array')); + self :: add_js_file(App::get('templates.included_js_files', null, 'array'), 500); // Messages self :: assign('errors', self :: get_errors()); @@ -518,7 +532,8 @@ class Tpl { // Files inclusions self :: assign('css', self :: $css_files); - self :: assign('js', self :: $js_files); + asort(self :: $js_files, SORT_NUMERIC); + self :: assign('js', array_keys(self :: $js_files)); // I18n text domains self :: assign('CORE_TEXT_DOMAIN', I18n :: CORE_TEXT_DOMAIN); @@ -647,7 +662,7 @@ class Tpl { $data=null, $error_code=null, $pretty=null, $keep_messages_on_success=false ) { if (!is_array($data)) - $data = array(); + $data = []; // Adjust HTTP error code on unsuccessful request (or if custom error code is provided) if ( @@ -876,7 +891,7 @@ class Tpl { public static function static_directories($details=false) { if ($details) return self :: $static_directories; - $result = array(); + $result = []; foreach(self :: $static_directories as $root_url => $dirs) foreach(array_keys($dirs) as $dir) if (!in_array($dir, $result)) @@ -901,7 +916,7 @@ class Tpl { } if (!array_key_exists($root_url, self :: $static_directories)) { - self :: $static_directories[$root_url] = array(); + self :: $static_directories[$root_url] = []; if (is_null($priority)) $priority = 100; $pattern = "#^(?P$root_url)(?P.*)#"; Log :: trace( diff --git a/templates/empty.tpl b/templates/empty.tpl index 13a8fdd..cdee8b1 100644 --- a/templates/empty.tpl +++ b/templates/empty.tpl @@ -24,7 +24,6 @@ - {foreach $css as $path} {/foreach} @@ -143,8 +142,6 @@ - - {foreach $js as $path}