fix(template): Update variable fallback behavior to return empty string for missing variables

This commit is contained in:
Yury Pikhtarev 2025-06-21 11:41:24 +04:00
commit aab7d1b9ad
No known key found for this signature in database
2 changed files with 13 additions and 8 deletions

View file

@ -421,7 +421,7 @@ class Template
// Append the variable reference.
$varref .= "['$varname']";
$varref = "<?php echo isset($varref) ? $varref : '" . $namespace . ".$varname'; ?>";
$varref = '<?php echo isset(' . $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', '<?php echo isset($L[\'$2\']) ? $L[\'$2\'] : (isset($V[\'$1\']) ? $V[\'$1\'] : \'$1\'); ?>', $code);
// Handle L_ language variables specifically - show plain text when not found
$code = preg_replace('#\{(L_([a-z0-9\-_]+?))\}#i', '<?php echo isset($L[\'$2\']) ? $L[\'$2\'] : \'$1\'; ?>', $code);
// Handle PHP variables
$code = preg_replace('#\{(\$[a-z_][a-z0-9_$\->\'\"\.\[\]]*?)\}#i', '<?php echo isset($1) ? $1 : \'\'; ?>', $code);
// Handle constants
$code = preg_replace('#\{(\#([a-z_][a-z0-9_]*?)\#)\}#i', '<?php echo defined(\'$2\') ? $2 : \'\'; ?>', $code);
$code = preg_replace('#\{([a-z0-9\-_]+?)\}#i', '<?php echo isset($V[\'$1\']) ? $V[\'$1\'] : \'\'; ?>', $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', '<?php echo isset($V[\'$1\']) ? $V[\'$1\'] : \'\'; ?>', $code);
return $code;
}

View file

@ -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 = "<?php echo isset(\$testblock_item['MISSING_VAR']) ? \$testblock_item['MISSING_VAR'] : 'testblock.MISSING_VAR'; ?>";
// Block variables should show empty string when missing, not the variable name
$expectedFormat = "<?php echo isset(\$testblock_item['MISSING_VAR']) ? \$testblock_item['MISSING_VAR'] : ''; ?>";
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 = "<?php echo isset(\$news_item['TITLE']) ? \$news_item['TITLE'] : 'news.TITLE'; ?>";
// Block variables should show empty string when missing, not the variable name
$expectedFormat = "<?php echo isset(\$news_item['TITLE']) ? \$news_item['TITLE'] : ''; ?>";
expect($result)->toBe($expectedFormat);
});