From 40f0dda0e6d4370c96dd6cbb9d6a1a08af969789 Mon Sep 17 00:00:00 2001 From: Uwe Tews Date: Tue, 7 Jul 2015 18:18:19 +0200 Subject: [PATCH] - new feature multiple indices on file: resource --- NEW_FEATURES.txt | 14 +++++ change_log.txt | 2 + libs/Smarty.class.php | 8 +-- .../smarty_internal_resource_file.php | 54 ++++++++++--------- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/NEW_FEATURES.txt b/NEW_FEATURES.txt index b72b9a33..a62e8c1c 100644 --- a/NEW_FEATURES.txt +++ b/NEW_FEATURES.txt @@ -4,6 +4,20 @@ This file contains a brief description of new features which have been added to Smarty 3.1.28 + Modifier regex_replace + ====================== + An optional limit parameter was added + + fetch() and display() + ===================== + The fetch() and display() methods of the template object accept now optionally the same paramter + as the corresponding Smarty methods the get tne content of another template. + + File: resource + ============== + Multiple template_dir entries can now be selected by a comma separated list of indices. + The template_dir array is searched in the order of the indices. (could be used to change the default search order) + Filter support ============== Optional filter names diff --git a/change_log.txt b/change_log.txt index 967c3e9b..5e6e2b26 100644 --- a/change_log.txt +++ b/change_log.txt @@ -2,6 +2,8 @@ 07.07.2015 - improvement allow fetch() or display() called on a template object to get output from other template like $template->fetch('foo.tpl') https://github.com/smarty-php/smarty/issues/70 + - improvement Added $limit parameter to regex_replace modifier #71 + - new feature multiple indices on file: resource 06.07.2015 - optimize {block} compilation diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 17502bd8..8f96d053 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.28-dev/25'; + const SMARTY_VERSION = '3.1.28-dev/26'; /** * define variable scopes @@ -1396,8 +1396,10 @@ class Smarty extends Smarty_Internal_TemplateBase if ($realpath !== null && $path[0] !== '/' && $path[1] !== ':') { $path = getcwd() . DS . $path; } - while (preg_match(isset($pattern) ? $pattern : $pattern = '#([\\\/][.]+[\\\/])|[' . (DS == '/' ? '\\\\' : '/') . ']|[\\\/]{2,}#', $path)) { - $path = preg_replace(isset($pattern2) ? $pattern2 : $pattern2 = '#([\\\/]+([.][\\\/]+)+)|([\\\/]+([^\\\/]+[\\\/]+){2}([.][.][\\\/]+){2})|([\\\/]+[^\\\/]+[\\\/]+[.][.][\\\/]+)|[\\\/]{2,}|[' . (DS == '/' ? '\\\\' : '/') . ']+#', DS, $path); + while (preg_match(isset($pattern) ? $pattern : $pattern = '#([\\\/][.]+[\\\/])|[' . (DS == '/' ? '\\\\' : '/') . + ']|[\\\/]{2,}#', $path)) { + $path = preg_replace(isset($pattern2) ? $pattern2 : $pattern2 = '#([\\\/]+([.][\\\/]+)+)|([\\\/]+([^\\\/]+[\\\/]+){2}([.][.][\\\/]+){2})|([\\\/]+[^\\\/]+[\\\/]+[.][.][\\\/]+)|[\\\/]{2,}|[' . + (DS == '/' ? '\\\\' : '/') . ']+#', DS, $path); } if ($realpath === false && ($path[0] == '/' || $path[1] == ':')) { $path = str_ireplace(getcwd(), '.', $path); diff --git a/libs/sysplugins/smarty_internal_resource_file.php b/libs/sysplugins/smarty_internal_resource_file.php index fa134e5a..92b1a97e 100644 --- a/libs/sysplugins/smarty_internal_resource_file.php +++ b/libs/sysplugins/smarty_internal_resource_file.php @@ -53,31 +53,33 @@ class Smarty_Internal_Resource_File extends Smarty_Resource $_directories = $source->smarty->getTemplateDir(null, $source->isConfig); // template_dir index? if ($file[0] == '[' && preg_match('#^\[([^\]]+)\](.+)$#', $file, $fileMatch)) { - $index = $fileMatch[1]; - $_directory = null; - // try string indexes - if (isset($_directories[$index])) { - $_directory = $_directories[$index]; - } elseif (is_numeric($index)) { - // try numeric index - $index = (int) $index; + $file = $fileMatch[2]; + $_indices = explode(',', $fileMatch[1]); + $_index_dirs = array(); + foreach ($_indices as $index) { + $index = trim($index); + // try string indexes if (isset($_directories[$index])) { - $_directory = $_directories[$index]; - } else { - // try at location index - $keys = array_keys($_directories); - $_directory = $_directories[$keys[$index]]; + $_index_dirs[] = $_directories[$index]; + } elseif (is_numeric($index)) { + // try numeric index + $index = (int) $index; + if (isset($_directories[$index])) { + $_index_dirs[] = $_directories[$index]; + } else { + // try at location index + $keys = array_keys($_directories); + if (isset($_directories[$keys[$index]])) { + $_index_dirs[] = $_directories[$keys[$index]]; + } + } } } - if ($_directory) { - $path = $_directory . $fileMatch[2]; - $path = $source->smarty->_realpath($path); - if (is_file($path)) { - return $path; - } - } else { + if (empty($_index_dirs)) { // index not found return false; + } else { + $_directories = $_index_dirs; } } @@ -85,13 +87,15 @@ class Smarty_Internal_Resource_File extends Smarty_Resource foreach ($_directories as $_directory) { $path = $_directory . $file; if (is_file($path)) { - return $source->smarty->_realpath($path, true); + return $source->smarty->_realpath($path); } } - // Could be relative to cwd - $path = $source->smarty->_realpath($file); - if (is_file($path)) { - return $path; + if (!isset($_index_dirs)) { + // Could be relative to cwd + $path = $source->smarty->_realpath($file, true); + if (is_file($path)) { + return $path; + } } // Use include path ? if ($source->smarty->use_include_path) {