From c0c5deeddacdbc77f99d1166447eb8ca50ea8f88 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Wed, 8 Mar 2023 13:06:04 +0100 Subject: [PATCH] Add TplCore Smarty templates ressource to make core templates easily overloadable --- example/templates/empty.tpl | 10 ++++++++++ src/Tpl.php | 16 +++++++++++----- src/TplSmartyResource.php | 21 ++++++++++++++------- 3 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 example/templates/empty.tpl diff --git a/example/templates/empty.tpl b/example/templates/empty.tpl new file mode 100644 index 0000000..3f1411c --- /dev/null +++ b/example/templates/empty.tpl @@ -0,0 +1,10 @@ +{extends file='TplCore:empty.tpl'} +{block name="navbar-extra-content"} + +{/block} diff --git a/src/Tpl.php b/src/Tpl.php index fed282f..685c8f1 100644 --- a/src/Tpl.php +++ b/src/Tpl.php @@ -160,6 +160,7 @@ class Tpl { self :: $smarty->setTemplateDir(self :: $core_templates_directory); self :: $smarty->setCompileDir($templates_c_dir); self :: $smarty->registerResource('Tpl', new TplSmartyResource()); + self :: $smarty->registerResource('TplCore', new TplSmartyResource(true)); $debug_ajax = App::get('templates.debug_ajax', null, 'bool'); self :: $_debug_ajax = boolval($debug_ajax); Log :: register_fatal_error_handler(array('\\EesyPHP\\Tpl', 'fatal_error')); @@ -617,10 +618,13 @@ class Tpl { /** * Resolve templates path against registered templates directories * @param string $path + * @param bool $core_only Core only mode (optional, default: false) * @return string|false */ - public static function resolve_templates_path($path) { + public static function resolve_templates_path($path, $core_only=false) { foreach(array_keys(self :: $templates_directories) as $dir) { + if ($core_only && $dir != self :: $core_templates_directory) + continue; $fullpath = "$dir/$path"; if (file_exists($fullpath)) { Log::trace('Templates file "%s" resolved as "%s"', $path, $fullpath); @@ -635,11 +639,12 @@ class Tpl { * Return the content of a Smarty template file. * * @param string $template The template name (eg: base.tpl) + * @param bool $core_only Core only mode (optional, default: false) * * @return string The content of the Smarty template file **/ - public static function get_template_source($template) { - $path = self :: resolve_templates_path($template); + public static function get_template_source($template, $core_only=false) { + $path = self :: resolve_templates_path($template, $core_only); if (!is_readable($path)) { // No error return with Smarty3 and highter because it's call // template name in lower first systematically @@ -653,11 +658,12 @@ class Tpl { * template file. * * @param string $template The template name (eg: empty.tpl) + * @param bool $core_only Core only mode (optional, default: false) * * @return int|null The timestamp of the last change of the Smarty template file **/ - public static function get_template_timestamp($template) { - $path = self :: resolve_templates_path($template); + public static function get_template_timestamp($template, $core_only=false) { + $path = self :: resolve_templates_path($template, $core_only); if (is_file($path)) { $time = filemtime($path); if ($time) diff --git a/src/TplSmartyResource.php b/src/TplSmartyResource.php index 808e366..64875f5 100644 --- a/src/TplSmartyResource.php +++ b/src/TplSmartyResource.php @@ -11,10 +11,17 @@ use Smarty_Resource_Custom; */ class TplSmartyResource extends Smarty_Resource_Custom { - // prepared fetch() statement - protected $fetch; - // prepared fetchTimestamp() statement - protected $mtime; + // Core only templates + protected $core_only; + + /** + * Constructor + * @param bool $core_only Core only mode (optional, default: false) + * @return void + */ + public function __construct($core_only=false) { + $this -> core_only = $core_only; + } /** * Fetch a template and its modification time @@ -25,8 +32,8 @@ class TplSmartyResource extends Smarty_Resource_Custom { * @return void */ protected function fetch($name, &$source, &$mtime) { - $source = Tpl :: get_template_source($name); - $mtime = Tpl :: get_template_timestamp($name); + $source = Tpl :: get_template_source($name, $this -> core_only); + $mtime = Tpl :: get_template_timestamp($name, $this -> core_only); } /** @@ -39,6 +46,6 @@ class TplSmartyResource extends Smarty_Resource_Custom { * @return integer timestamp (epoch) the template was modified */ protected function fetchTimestamp($name) { - return Tpl :: get_template_timestamp($name); + return Tpl :: get_template_timestamp($name, $this -> core_only); } }