Skip to content

Commit 0762097

Browse files
committed
Explicitly treat null as an empty string when interpolating.
Fixes a deprecation warning on PHP 8.1+, and saves us some unnecessary escaping calls! Thanks for your help @scr4bble :) Fixes #383
1 parent 7fd191f commit 0762097

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/Mustache/Compiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ private static function onlyBlockArgs(array $node)
506506

507507
const VARIABLE = '
508508
$value = $this->resolveValue($context->%s(%s), $context);%s
509-
$buffer .= %s%s;
509+
$buffer .= %s($value === null ? \'\' : %s);
510510
';
511511

512512
/**

test/Mustache/Test/CompilerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function getCompileValues()
5656
array(
5757
"\nclass Monkey extends Mustache_Template",
5858
'$value = $this->resolveValue($context->find(\'name\'), $context);',
59-
'$buffer .= $indent . call_user_func($this->mustache->getEscape(), $value);',
59+
'$buffer .= $indent . ($value === null ? \'\' : call_user_func($this->mustache->getEscape(), $value));',
6060
'return $buffer;',
6161
),
6262
),
@@ -76,7 +76,7 @@ public function getCompileValues()
7676
array(
7777
"\nclass Monkey extends Mustache_Template",
7878
'$value = $this->resolveValue($context->find(\'name\'), $context);',
79-
'$buffer .= $indent . htmlspecialchars($value, ' . ENT_COMPAT . ', \'ISO-8859-1\');',
79+
'$buffer .= $indent . ($value === null ? \'\' : htmlspecialchars($value, ' . ENT_COMPAT . ', \'ISO-8859-1\'));',
8080
'return $buffer;',
8181
),
8282
),
@@ -96,7 +96,7 @@ public function getCompileValues()
9696
array(
9797
"\nclass Monkey extends Mustache_Template",
9898
'$value = $this->resolveValue($context->find(\'name\'), $context);',
99-
'$buffer .= $indent . htmlspecialchars($value, ' . ENT_QUOTES . ', \'ISO-8859-1\');',
99+
'$buffer .= $indent . ($value === null ? \'\' : htmlspecialchars($value, ' . ENT_QUOTES . ', \'ISO-8859-1\'));',
100100
'return $buffer;',
101101
),
102102
),
@@ -123,7 +123,7 @@ public function getCompileValues()
123123
"\nclass Monkey extends Mustache_Template",
124124
"\$buffer .= \$indent . 'foo\n';",
125125
'$value = $this->resolveValue($context->find(\'name\'), $context);',
126-
'$buffer .= htmlspecialchars($value, ' . ENT_COMPAT . ', \'UTF-8\');',
126+
'$buffer .= ($value === null ? \'\' : htmlspecialchars($value, ' . ENT_COMPAT . ', \'UTF-8\'));',
127127
'$value = $this->resolveValue($context->last(), $context);',
128128
'$buffer .= \'\\\'bar\\\'\';',
129129
'return $buffer;',

0 commit comments

Comments
 (0)