From 11ef5dcfff2ef0efc3e5c805c0a11b7dc08f83f5 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Tue, 17 Oct 2023 02:45:22 +0900 Subject: [PATCH] Add support for Blade-style @class and @style directives --- .../parsers/template/TemplateParser_v2.php | 21 ++++++++++++++++++- tests/_data/template/v2example.html | 5 ++++- tests/_data/template/v2result1.php | 5 ++++- tests/_data/template/v2result2.php | 5 ++++- .../parsers/TemplateParserV2Test.php | 14 +++++++++++-- 5 files changed, 44 insertions(+), 6 deletions(-) diff --git a/common/framework/parsers/template/TemplateParser_v2.php b/common/framework/parsers/template/TemplateParser_v2.php index 75301918f..f2d1269b3 100644 --- a/common/framework/parsers/template/TemplateParser_v2.php +++ b/common/framework/parsers/template/TemplateParser_v2.php @@ -421,7 +421,7 @@ class TemplateParser_v2 $tpl .= '$__tpl = new \Rhymix\Framework\Template($__dir, $__path, "' . $extension . '"); '; $tpl .= '$__tpl->setVars([(string)$__varname => $__var]); ' ; $tpl .= 'echo $__tpl->compile(); endforeach; })(' . $dir . ', ' . $args . '); ?>'; - return $tpl; + return self::_escapeVars($tpl); }, $content); return $content; @@ -754,6 +754,25 @@ class TemplateParser_v2 return sprintf(' %s="%s"', $condition, $match[1], $match[1]); }, $content); + // Convert Blade-style @class and @style conditions. + $regexp = '#\s*(? $__val): '; + $tpl .= 'if (is_numeric($__key)): $__values[] = $__val; '; + $tpl .= 'elseif ($__val): $__values[] = $__key; '; + $tpl .= 'endif; endforeach; '; + $tpl .= 'if ($__values): '; + $tpl .= 'echo \' ' . $match[1] . '="\'; '; + $tpl .= 'echo htmlspecialchars(implode(\'' . $delimiter . '\', $__values), \ENT_QUOTES, \'UTF-8\', false); '; + $tpl .= 'echo \'"\'; '; + $tpl .= 'endif; })(' . $defs . '); ?>'; + return self::_escapeVars($tpl); + }, $content); + return $content; } diff --git a/tests/_data/template/v2example.html b/tests/_data/template/v2example.html index e108bc56a..6de1b3d32 100644 --- a/tests/_data/template/v2example.html +++ b/tests/_data/template/v2example.html @@ -46,7 +46,10 @@

The full class name is {get_class(new Push)|escape}, {Push::class} really.

@enddesktop -
+
+ $foo, 'text-blue' => false, 'bg-white' => true])> + Context::get('bar'), 'padding: 2rem' => false])> +