Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/View/Antlers/Language/Runtime/ModifierManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class ModifierManager
*/
protected static $nullAwareModifiers = [
'bool_string',
'json',
'to_json',
];

public static function isModifier(ParameterNode $node)
Expand Down
17 changes: 17 additions & 0 deletions tests/Antlers/Runtime/CoreModifiersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,23 @@ public function test_bool_string_outputs_false_for_null()
$this->assertSame('FALSE', $this->renderString('{{ empty_field | bool_string | upper }}', $data, true));
$this->assertSame('true', $this->renderString('{{ filled_field | bool_string }}', $data, true));
}

public function test_to_json_outputs_null_for_null()
{
$data = [
'bare' => null,
'empty_field' => new Value(null, 'empty_field', new Text),
'filled_field' => new Value('yes', 'filled_field', new Text),
];

$this->assertSame('null', $this->renderString('{{ bare | to_json }}', $data, true));
$this->assertSame('null', $this->renderString('{{ empty_field | to_json }}', $data, true));
$this->assertSame('null', $this->renderString('{{ empty_field | json }}', $data, true));
$this->assertSame('null', $this->renderString('{{ missing | to_json }}', $data, true));
$this->assertSame('null', $this->renderString("{{ empty_field | to_json('pretty') }}", $data, true));
$this->assertSame('NULL', $this->renderString('{{ empty_field | to_json | upper }}', $data, true));
$this->assertSame('"yes"', $this->renderString('{{ filled_field | to_json }}', $data, true));
}
}

class SimpleEntryObject implements Arrayable
Expand Down
Loading