Skip to content

Commit

Permalink
Return null instead of blank string for undefined variables
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorejb committed Dec 20, 2024
1 parent 0d0bb06 commit 8640ac2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
18 changes: 7 additions & 11 deletions src/Handlebars/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,10 @@ public function get(string $variableName, bool $strict = false)
}
if (count($this->stack) < $level) {
if ($strict) {
throw new InvalidArgumentException(
'can not find variable in context'
);
throw new InvalidArgumentException('can not find variable in context');
}

return '';
return null;
}
end($this->stack);
while ($level) {
Expand All @@ -226,18 +224,16 @@ public function get(string $variableName, bool $strict = false)
$current = current($this->stack);
if (!$variableName) {
if ($strict) {
throw new InvalidArgumentException(
'can not find variable in context'
);
throw new InvalidArgumentException('can not find variable in context');
}
return '';
return null;
} elseif ($variableName == '.' || $variableName == 'this') {
return $current;
} else {
$chunks = explode('.', $variableName);
foreach ($chunks as $chunk) {
if (is_string($current) and $current == '') {
return $current;
if (is_null($current)) {
return null;
}
$current = $this->findVariableInContext($current, $chunk, $strict);
}
Expand Down Expand Up @@ -324,7 +320,7 @@ public function getDataVariable(string $variableName, bool $strict = false)
*/
private function findVariableInContext($variable, string $inside, bool $strict = false)
{
$value = '';
$value = null;
if (($inside !== '0' && empty($inside)) || ($inside == 'this')) {
return $variable;
} elseif (is_array($variable)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Handlebars/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private function partial(Context $context, array $current): string
private function variables(Context $context, array $current, bool $escaped): string
{
$name = $current[Tokenizer::NAME];
$value = $context->get($name);
$value = (string) $context->get($name);

// If @data variables are enabled, use the more complex algorithm for handling the the variables otherwise
// use the previous version.
Expand Down
33 changes: 15 additions & 18 deletions tests/Handlebars/HandlebarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
class HandlebarsTest extends PHPUnit\Framework\TestCase
{
/**
* Test basic tags
*
* @dataProvider simpleTagdataProvider
*/
public function testBasicTags(string $src, array $data, string $result): void
{
$engine = new \Handlebars\Handlebars();
$this->assertEquals($result, $engine->render($src, $data));
$this->assertSame($result, $engine->render($src, $data));
}

/**
* Simple tag provider
*/
public function simpleTagdataProvider(): array
{
return array(
Expand All @@ -34,24 +29,29 @@ public function simpleTagdataProvider(): array
array('data' => array('key' => 'result')),
'result'
),
array(
'{{non.existent}}',
array(),
''
),
);
}

/**
* Test helpers (internal helpers)
*
* @dataProvider internalHelpersdataProvider
* @dataProvider internalHelpersDataProvider
*/
public function testSimpleHelpers(string $src, array $data, string $result): void
{
$engine = new \Handlebars\Handlebars();
$this->assertEquals($result, $engine->render($src, $data));
$this->assertSame($result, $engine->render($src, $data));
}

/**
* Simple helpers provider
*/
public function internalHelpersdataProvider(): array
public function internalHelpersDataProvider(): array
{
return [
[
Expand Down Expand Up @@ -187,14 +187,14 @@ public function internalHelpersdataProvider(): array
*/
public function testDataVariables(string $src, array $data, string $result, bool $enableDataVariables): void
{
$engine = new \Handlebars\Handlebars(array(
$engine = new \Handlebars\Handlebars([
'enableDataVariables'=> $enableDataVariables,
));
]);

$this->assertEquals($result, $engine->render($src, $data));
$this->assertSame($result, $engine->render($src, $data));
}

public function testDataVariables1()
public function testDataVariables1(): void
{
$object = new stdClass;
$object->{'@first'} = 'apple';
Expand Down Expand Up @@ -354,7 +354,7 @@ public function internalDataVariablesDataProvider(): array
/**
* Management helpers
*/
public function testHelpersManagement()
public function testHelpersManagement(): void
{
$helpers = new \Handlebars\Helpers(array('test' => function () {
}), false);
Expand All @@ -365,10 +365,7 @@ public function testHelpersManagement()
$this->assertFalse($engine->hasHelper('test'));
}

/**
* Custom helper test
*/
public function testCustomHelper()
public function testCustomHelper(): void
{
$engine = new \Handlebars\Handlebars();
$engine->addHelper('test', function () {
Expand Down

0 comments on commit 8640ac2

Please sign in to comment.