From aab7d1b9ad320f4d4b32c2fa463d814a95c0fb32 Mon Sep 17 00:00:00 2001 From: Yury Pikhtarev Date: Sat, 21 Jun 2025 11:41:24 +0400 Subject: [PATCH] fix(template): Update variable fallback behavior to return empty string for missing variables --- src/Legacy/Template.php | 11 ++++++++--- tests/Unit/Legacy/TemplateGracefulFallbackTest.php | 10 +++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Legacy/Template.php b/src/Legacy/Template.php index 83ec25199..c5acceeeb 100644 --- a/src/Legacy/Template.php +++ b/src/Legacy/Template.php @@ -421,7 +421,7 @@ class Template // Append the variable reference. $varref .= "['$varname']"; - $varref = ""; + $varref = ''; return $varref; } @@ -766,10 +766,15 @@ class Template $code = str_replace($search, $replace, $code); } // This will handle the remaining root-level varrefs - $code = preg_replace('#\{(L_([a-z0-9\-_]+?))\}#i', '', $code); + // Handle L_ language variables specifically - show plain text when not found + $code = preg_replace('#\{(L_([a-z0-9\-_]+?))\}#i', '', $code); + // Handle PHP variables $code = preg_replace('#\{(\$[a-z_][a-z0-9_$\->\'\"\.\[\]]*?)\}#i', '', $code); + // Handle constants $code = preg_replace('#\{(\#([a-z_][a-z0-9_]*?)\#)\}#i', '', $code); - $code = preg_replace('#\{([a-z0-9\-_]+?)\}#i', '', $code); + // Handle simple variables (but NOT variables with dots - those should be handled by block processing) + // Only match variables that don't contain dots + $code = preg_replace('#\{([a-z0-9\-_]+)\}#i', '', $code); return $code; } diff --git a/tests/Unit/Legacy/TemplateGracefulFallbackTest.php b/tests/Unit/Legacy/TemplateGracefulFallbackTest.php index ff57a55b3..9f3df1466 100644 --- a/tests/Unit/Legacy/TemplateGracefulFallbackTest.php +++ b/tests/Unit/Legacy/TemplateGracefulFallbackTest.php @@ -217,14 +217,14 @@ describe('Template Text Compilation - Graceful Fallback', function () { describe('Template Block Variable Fallback', function () { - it('shows missing block variables as original syntax', function () { + it('shows missing block variables as empty string', function () { $namespace = 'testblock'; $varname = 'MISSING_VAR'; $result = $this->template->generate_block_varref($namespace . '.', $varname); - // Verify the exact expected fallback output format string - $expectedFormat = ""; + // Block variables should show empty string when missing, not the variable name + $expectedFormat = ""; expect($result)->toBe($expectedFormat); }); @@ -234,8 +234,8 @@ describe('Template Block Variable Fallback', function () { $result = $this->template->generate_block_varref($namespace . '.', $varname); - // Verify the exact expected fallback output format string - $expectedFormat = ""; + // Block variables should show empty string when missing, not the variable name + $expectedFormat = ""; expect($result)->toBe($expectedFormat); });