From 514d99beaaad028fe9bf09b3188ab2a281d0f3f8 Mon Sep 17 00:00:00 2001 From: Uwe Tews Date: Thu, 17 May 2018 06:27:35 +0200 Subject: [PATCH] - improvement do not compute total property in {foreach} if not needed https://github.com/smarty-php/smarty/issues/443 --- change_log.txt | 3 ++- libs/Smarty.class.php | 2 +- libs/sysplugins/smarty_internal_runtime_foreach.php | 7 +++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/change_log.txt b/change_log.txt index 5a81e84c..3d3f2e34 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,5 +1,6 @@ -===== 3.1.33-dev-2 ===== +===== 3.1.33-dev-3 ===== 17.05.2018 + - improvement do not compute total property in {foreach} if not needed https://github.com/smarty-php/smarty/issues/443 - bugfix plugins may not be loaded when setMergeCompiledIncludes is true https://github.com/smarty-php/smarty/issues/435 26.04.2018 diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index f9737b28..2116f74d 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -112,7 +112,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.33-dev-2'; + const SMARTY_VERSION = '3.1.33-dev-3'; /** * define variable scopes */ diff --git a/libs/sysplugins/smarty_internal_runtime_foreach.php b/libs/sysplugins/smarty_internal_runtime_foreach.php index 8b60768b..4fa29d5b 100644 --- a/libs/sysplugins/smarty_internal_runtime_foreach.php +++ b/libs/sysplugins/smarty_internal_runtime_foreach.php @@ -37,17 +37,20 @@ class Smarty_Internal_Runtime_Foreach public function init(Smarty_Internal_Template $tpl, $from, $item, $needTotal = false, $key = null, $name = null, $properties = array()) { + $needTotal = $needTotal || isset($properties[ 'total' ]); $saveVars = array(); $total = null; if (!is_array($from)) { if (is_object($from)) { - $total = $this->count($from); + if ($needTotal) { + $total = $this->count($from); + } } else { settype($from, 'array'); } } if (!isset($total)) { - $total = empty($from) ? 0 : (($needTotal || isset($properties[ 'total' ])) ? count($from) : 1); + $total = empty($from) ? 0 : ($needTotal ? count($from) : 1); } if (isset($tpl->tpl_vars[ $item ])) { $saveVars[ 'item' ] = array($item,