Skip to content

Commit 762a4ce

Browse files
committed
Fix extending context with partial hash arguments
Also fixed stringification of arrays returned by block helpers.
1 parent ed20a60 commit 762a4ce

3 files changed

Lines changed: 18 additions & 13 deletions

File tree

src/HelperOptions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ final class HelperOptions
2020
* @param array<mixed> $data
2121
* @param array<mixed> $hash
2222
* @param array<mixed> $outerBlockParams outer block param stack, passed as trailing elements of the stack
23+
* @internal
2324
*/
2425
public function __construct(
2526
public mixed &$scope,

src/Runtime.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -375,18 +375,10 @@ public static function section(RuntimeContext $cx, mixed $value, mixed $in, ?Clo
375375
/**
376376
* Merge context objects, equivalent to Utils.extend() in Handlebars.js.
377377
*/
378-
public static function extend(mixed $a, mixed $b): mixed
378+
private static function extend(mixed $a, mixed $b): mixed
379379
{
380380
if (is_array($b)) {
381-
if ($a === null || is_int($a)) {
382-
return $b;
383-
} elseif (is_array($a)) {
384-
return array_replace($a, $b);
385-
} elseif (is_object($a)) {
386-
foreach ($b as $i => $v) {
387-
$a->$i = $v;
388-
}
389-
}
381+
return is_array($a) ? array_replace($a, $b) : $b;
390382
}
391383
return $a;
392384
}
@@ -560,7 +552,7 @@ private static function resolveBlockResult(RuntimeContext $cx, mixed $result, mi
560552

561553
// Arrays stringify like JS Array.prototype.toString(), regardless of fn block.
562554
if (is_array($result)) {
563-
return implode(',', $result);
555+
return self::raw($result);
564556
}
565557

566558
if ($cb === null) {

tests/RegressionTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,8 +766,13 @@ public static function helperProvider(): array
766766
],
767767
'block helper returning truthy non-string: stringified like JS' => [
768768
'template' => '{{#helper}}block{{/helper}}',
769-
'helpers' => ['helper' => fn() => ['truthy', 'array']],
770-
'expected' => 'truthy,array',
769+
'helpers' => ['helper' => fn() => ['a', ['ba', 'bb'], 'c']],
770+
'expected' => 'a,ba,bb,c',
771+
],
772+
'block helper returning non-list array: stringified like JS' => [
773+
'template' => '{{#helper}}block{{/helper}}',
774+
'helpers' => ['helper' => fn() => ['a' => 'foo', 'b' => 'bar']],
775+
'expected' => '[object Object]',
771776
],
772777
'inverted known block helper returning truthy non-string: stringified like JS' => [
773778
'template' => '{{^helper}}block{{/helper}}',
@@ -1142,6 +1147,13 @@ public static function partialProvider(): array
11421147
'data' => ['condition' => false],
11431148
'expected' => ' Failover',
11441149
],
1150+
1151+
'partial hash arguments replace scalar context' => [
1152+
'template' => '{{#with str}}{{> myPartial key="val"}}{{/with}}',
1153+
'partials' => ['myPartial' => '{{key}}'],
1154+
'data' => ['str' => 'hello'],
1155+
'expected' => 'val',
1156+
],
11451157
];
11461158
}
11471159

0 commit comments

Comments
 (0)