Merge pull request #11238 from creative-commoners/pulls/5/viewable-scalar

ENH Rendering scalars in ArrayList in templates
This commit is contained in:
Guy Sartorelli 2024-05-15 17:33:13 +12:00 committed by GitHub
commit 8afe1adc57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 5 deletions

View File

@ -376,7 +376,7 @@ class SSViewer_DataPresenter extends SSViewer_Scope
// Check if the method to-be-called exists on the target object - if so, don't check any further // Check if the method to-be-called exists on the target object - if so, don't check any further
// injection locations // injection locations
$on = $this->itemIterator ? $this->itemIterator->current() : $this->item; $on = $this->getItem();
if (is_object($on) && (isset($on->$property) || method_exists($on, $property ?? ''))) { if (is_object($on) && (isset($on->$property) || method_exists($on, $property ?? ''))) {
return []; return [];
} }

View File

@ -5,6 +5,11 @@ namespace SilverStripe\View;
use ArrayIterator; use ArrayIterator;
use Countable; use Countable;
use Iterator; use Iterator;
use SilverStripe\ORM\FieldType\DBBoolean;
use SilverStripe\ORM\FieldType\DBText;
use SilverStripe\ORM\FieldType\DBFloat;
use SilverStripe\ORM\FieldType\DBInt;
use SilverStripe\ORM\FieldType\DBField;
/** /**
* This tracks the current scope for an SSViewer instance. It has three goals: * This tracks the current scope for an SSViewer instance. It has three goals:
@ -121,7 +126,11 @@ class SSViewer_Scope
*/ */
public function getItem() public function getItem()
{ {
return $this->itemIterator ? $this->itemIterator->current() : $this->item; $item = $this->itemIterator ? $this->itemIterator->current() : $this->item;
if (is_scalar($item)) {
$item = $this->convertScalarToDBField($item);
}
return $item;
} }
/** /**
@ -176,7 +185,7 @@ class SSViewer_Scope
*/ */
public function getObj($name, $arguments = [], $cache = false, $cacheName = null) public function getObj($name, $arguments = [], $cache = false, $cacheName = null)
{ {
$on = $this->itemIterator ? $this->itemIterator->current() : $this->item; $on = $this->getItem();
return $on->obj($name, $arguments, $cache, $cacheName); return $on->obj($name, $arguments, $cache, $cacheName);
} }
@ -240,7 +249,7 @@ class SSViewer_Scope
*/ */
public function self() public function self()
{ {
$result = $this->itemIterator ? $this->itemIterator->current() : $this->item; $result = $this->getItem();
$this->resetLocalScope(); $this->resetLocalScope();
return $result; return $result;
@ -338,7 +347,7 @@ class SSViewer_Scope
*/ */
public function __call($name, $arguments) public function __call($name, $arguments)
{ {
$on = $this->itemIterator ? $this->itemIterator->current() : $this->item; $on = $this->getItem();
$retval = $on ? $on->$name(...$arguments) : null; $retval = $on ? $on->$name(...$arguments) : null;
$this->resetLocalScope(); $this->resetLocalScope();
@ -368,4 +377,14 @@ class SSViewer_Scope
{ {
return $this->upIndex; return $this->upIndex;
} }
private function convertScalarToDBField(bool|string|float|int $value): DBField
{
return match (gettype($value)) {
'boolean' => DBBoolean::create()->setValue($value),
'string' => DBText::create()->setValue($value),
'double' => DBFloat::create()->setValue($value),
'integer' => DBInt::create()->setValue($value),
};
}
} }

View File

@ -2218,4 +2218,16 @@ EOC;
$this->assertTrue(file_exists($cacheFile ?? ''), 'Cache file wasn\'t created when it was meant to'); $this->assertTrue(file_exists($cacheFile ?? ''), 'Cache file wasn\'t created when it was meant to');
unlink($cacheFile ?? ''); unlink($cacheFile ?? '');
} }
public function testPrimitivesConvertedToDBFields()
{
$data = new ArrayData([
// null value should not be rendered, though should also not throw exception
'Foo' => new ArrayList(['hello', true, 456, 7.89, null])
]);
$this->assertEqualIgnoringWhitespace(
'hello 1 456 7.89',
$this->render('<% loop $Foo %>$Me<% end_loop %>', $data)
);
}
} }