From 5e4792a348866c94a1cbe83196aa19babbdf6390 Mon Sep 17 00:00:00 2001 From: nzn1 Date: Wed, 19 Apr 2023 22:35:55 +0300 Subject: [PATCH 1/5] Update doT.php Fixed for PHP8 (used eval + lambda for creating the return function - needed eval for the argName feature) Added the option to set the argName as in the original doT.js by setting the public property "argName" to something like '$myvar' --- doT.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doT.php b/doT.php index 1795d73..49b3c4a 100644 --- a/doT.php +++ b/doT.php @@ -11,6 +11,7 @@ class doT { public $functionBody; private $functionCode; public $def; + public $argName = '$it'; public function resolveDefs ($block) { $me = $this; @@ -91,7 +92,9 @@ public function template ($string, $def) { $this->functionBody = $func; - return @create_function ('$it', $func); + return eval('return function (' . $this->argName . ') use ($func) { + return eval($func); + };'); } public function execute ($data) { From 499ea215f2fa0fc173023910edbca52d44c1a6b0 Mon Sep 17 00:00:00 2001 From: nzn1 Date: Wed, 19 Apr 2023 22:45:09 +0300 Subject: [PATCH 2/5] Update doT.php Fix UTF-8 characters breaking templates. --- doT.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/doT.php b/doT.php index 49b3c4a..28d593a 100644 --- a/doT.php +++ b/doT.php @@ -15,13 +15,13 @@ class doT { public function resolveDefs ($block) { $me = $this; - return preg_replace_callback ("/\{\{#([\s\S]+?)\}\}/", function ($m) use ($me) { + return preg_replace_callback ("/\{\{#([\s\S]+?)\}\}/u", function ($m) use ($me) { $d = $m[1]; $d = substr ($d, 4); if (!array_key_exists ($d, $me->def)) { return ""; } - if (preg_match ("/\{\{#([\s\S]+?)\}\}/", $me->def [$d])) { + if (preg_match ("/\{\{#([\s\S]+?)\}\}/u", $me->def [$d])) { return $me->resolveDefs ($me->def [$d], $me->def); } else { return $me->def [$d]; @@ -30,12 +30,12 @@ public function resolveDefs ($block) { } public function handleDotNotation ($string) { - $out = preg_replace ("/(\w+)\.(.*?)([\s,\)])/", "\$$1[\"$2\"]$3", $string); - $out = preg_replace ("/(\w+)\.([\w\.]*?)$/", "\$$1[\"$2\"] ", $out); - $out = preg_replace ("/\./", '"]["', $out); + $out = preg_replace ("/(\w+)\.(.*?)([\s,\)])/u", "\$$1[\"$2\"]$3", $string); + $out = preg_replace ("/(\w+)\.([\w\.]*?)$/u", "\$$1[\"$2\"] ", $out); + $out = preg_replace ("/\./u", '"]["', $out); // Special hideous case : shouldn't be committed - $out = preg_replace ("/^i /", ' $i ', $out); + $out = preg_replace ("/^i /u", ' $i ', $out); return $out; } @@ -46,20 +46,20 @@ public function template ($string, $def) { // deps if (empty ($def)) { - $func = preg_replace ("/\{\{#([\s\S]+?)\}\}/", "", $func); + $func = preg_replace ("/\{\{#([\s\S]+?)\}\}/u", "", $func); } else { $this->def = $def; $func = $this->resolveDefs ($func); } - $func = preg_replace ("/'|\\\/", "\\$&", $func); + $func = preg_replace ("/'|\\\/u", "\\$&", $func); // interpolate - $func = preg_replace_callback ("/\{\{=([\s\S]+?)\}\}/", function ($m) use ($me) { + $func = preg_replace_callback ("/\{\{=([\s\S]+?)\}\}/u", function ($m) use ($me) { return "' . " . $me->handleDotNotation ($m[1]) . " . '"; }, $func); // Conditional - $func = preg_replace_callback ("/\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/", function ($m) use ($me) { + $func = preg_replace_callback ("/\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/u", function ($m) use ($me) { $elsecase = $m[1]; $code = $m[2]; if ($elsecase) { From 94e2fbbf11661a5a3faec9769d681bd8359a833c Mon Sep 17 00:00:00 2001 From: nzn1 Date: Wed, 19 Apr 2023 22:46:14 +0300 Subject: [PATCH 3/5] Update doT.php Give default value to $def argument of template method --- doT.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doT.php b/doT.php index 28d593a..0eb5fe2 100644 --- a/doT.php +++ b/doT.php @@ -39,7 +39,7 @@ public function handleDotNotation ($string) { return $out; } - public function template ($string, $def) { + public function template ($string, $def = NULL) { $me = $this; $func = $string; From a884854b93aa7cf689ff6ca4710e788a07648c35 Mon Sep 17 00:00:00 2001 From: nzn1 Date: Wed, 19 Apr 2023 22:48:18 +0300 Subject: [PATCH 4/5] Update doT.php Remove unused $functionCode property and execute method --- doT.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/doT.php b/doT.php index 0eb5fe2..7e4b32d 100644 --- a/doT.php +++ b/doT.php @@ -9,7 +9,6 @@ class doT { public $functionBody; - private $functionCode; public $def; public $argName = '$it'; @@ -96,9 +95,4 @@ public function template ($string, $def = NULL) { return eval($func); };'); } - - public function execute ($data) { - return $this->functionCode ($data); - } - } From 1806a6a1d0a7e7bac28da141003b6758fc0871cd Mon Sep 17 00:00:00 2001 From: nzn1 Date: Wed, 19 Apr 2023 22:50:57 +0300 Subject: [PATCH 5/5] Update doT.php Corrected the preg_replace for handling single-quotes and slashes in templates --- doT.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doT.php b/doT.php index 7e4b32d..1950110 100644 --- a/doT.php +++ b/doT.php @@ -51,7 +51,7 @@ public function template ($string, $def = NULL) { $func = $this->resolveDefs ($func); } - $func = preg_replace ("/'|\\\/u", "\\$&", $func); + $func = preg_replace ("/'|\\\/u", "\\\\$0", $func); // interpolate $func = preg_replace_callback ("/\{\{=([\s\S]+?)\}\}/u", function ($m) use ($me) {