Skip to content

Commit ad9d559

Browse files
committed
Misc fixes
1 parent a3732b8 commit ad9d559

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

src/Core/Environment.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Codewithkyrian\Jinja\Runtime\FloatValue;
1212
use Codewithkyrian\Jinja\Runtime\FunctionValue;
1313
use Codewithkyrian\Jinja\Runtime\IntegerValue;
14+
use Codewithkyrian\Jinja\Runtime\KeywordArgumentsValue;
1415
use Codewithkyrian\Jinja\Runtime\NullValue;
1516
use Codewithkyrian\Jinja\Runtime\NumericValue;
1617
use Codewithkyrian\Jinja\Runtime\ObjectValue;
@@ -43,14 +44,12 @@ public function __construct(?Environment $parent = null)
4344
$this->parent = $parent;
4445

4546
$this->variables = [
46-
'namespace' => new FunctionValue(function ($args) {
47-
if (count($args) === 0) {
47+
'namespace' => new FunctionValue(function (?KeywordArgumentsValue $args = null) {
48+
if (!$args) {
4849
return new ObjectValue([]);
4950
}
50-
if (count($args) !== 1 || !($args[0] instanceof ObjectValue)) {
51-
throw new RuntimeException("`namespace` expects either zero arguments or a single object argument");
52-
}
53-
return $args[0];
51+
52+
return new ObjectValue($args->value);
5453
})
5554
];
5655

src/Runtime/FunctionValue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public function __construct(callable $value)
1717

1818
public function call(array $args, Environment $env): RuntimeValue
1919
{
20-
return call_user_func($this->value, $args, $env);
20+
return call_user_func_array($this->value, [...$args, $env]);
2121
}
2222
}

tests/Datasets/InterpreterDataset.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
const EXAMPLE_FOR_TEMPLATE_4 = "{% for item in seq -%}\n {{ item }}\n{%- endfor %}";
1010
const EXAMPLE_COMMENT_TEMPLATE = " {# comment #}\n {# {% if true %} {% endif %} #}\n";
1111
const EXAMPLE_OBJECT_LITERAL_TEMPLATE = "{% set obj = { 'key1': 'value1', 'key2': 'value2' } %}{{ obj.key1 }} - {{ obj.key2 }}";
12+
const EXAMPLE_OBJECT_GET = "{% set obj = { 'key1': 'value1', 'key2': 'value2' } %}{{ obj.get('key1') }} - {{ obj.get('key3', 'default') }}";
1213

1314
dataset('interpreterTestData', [
1415
// If tests
@@ -146,4 +147,18 @@
146147
'trim_blocks' => true,
147148
'target' => "value1 - value2",
148149
],
150+
'object get method (no strip or trim)' => [
151+
'template' => EXAMPLE_OBJECT_GET,
152+
'data' => [],
153+
'lstrip_blocks' => false,
154+
'trim_blocks' => false,
155+
'target' => "value1 - default",
156+
],
157+
'object get method (strip and trim)' => [
158+
'template' => EXAMPLE_OBJECT_GET,
159+
'data' => [],
160+
'lstrip_blocks' => true,
161+
'trim_blocks' => true,
162+
'target' => "value1 - default",
163+
],
149164
]);

0 commit comments

Comments
 (0)