From 75ce18bc18904d1215c7d021ac0ac18c0a7a5d42 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Wed, 28 Jul 2021 14:24:50 +0200 Subject: [PATCH] Foreach tag unset variables that have the value NULL --- .../template/TemplateScriptingCompiler.class.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/wcfsetup/install/files/lib/system/template/TemplateScriptingCompiler.class.php b/wcfsetup/install/files/lib/system/template/TemplateScriptingCompiler.class.php index 0193cde721..6a6c78f6a6 100644 --- a/wcfsetup/install/files/lib/system/template/TemplateScriptingCompiler.class.php +++ b/wcfsetup/install/files/lib/system/template/TemplateScriptingCompiler.class.php @@ -819,17 +819,13 @@ class TemplateScriptingCompiler { $foreachData['itemVar'] = $itemVar; $phpCode .= "\$this->foreachVars['{$hash}'] = [];\n"; - $phpCode .= "if (isset({$itemVar})) {\n"; - $phpCode .= "\$this->foreachVars['{$hash}']['item'] = {$itemVar};\n"; - $phpCode .= "}\n"; + $phpCode .= "\$this->foreachVars['{$hash}']['item'] = {$itemVar} ?? null;\n"; if (isset($args['key'])) { $keyVar = mb_substr($args['key'], 0, 1) != '$' ? "\$this->v[".$args['key']."]" : $args['key']; $foreachData['keyVar'] = $keyVar; - $phpCode .= "if (isset({$keyVar})) {\n"; - $phpCode .= "\$this->foreachVars['{$hash}']['key'] = {$keyVar};\n"; - $phpCode .= "}\n"; + $phpCode .= "\$this->foreachVars['{$hash}']['key'] = {$keyVar} ?? null;\n"; $phpCode .= "foreach (".$foreachHash." as {$keyVar} => {$itemVar}) {\n"; } @@ -862,13 +858,11 @@ class TemplateScriptingCompiler { // unset `item` and `key` variables and restore their sandboxed values $phpCode = "foreachVars['{$foreachData['hash']}']['item'])) {\n"; $phpCode .= "{$foreachData['itemVar']} = \$this->foreachVars['{$foreachData['hash']}']['item'];\n"; - $phpCode .= "}\n"; if (isset($foreachData['keyVar'])) { $phpCode .= "unset({$foreachData['keyVar']});"; - $phpCode .= "if (isset(\$this->foreachVars['{$foreachData['hash']}']['key'])) {\n"; + $phpCode .= "if (array_key_exists('key', \$this->foreachVars['{$foreachData['hash']}'])) {\n"; $phpCode .= "{$foreachData['keyVar']} = \$this->foreachVars['{$foreachData['hash']}']['key'];\n"; $phpCode .= "}\n"; } -- 2.20.1