diff --git a/CHANGELOG.md b/CHANGELOG.md index 95b7522..85baa18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - `@aware` variables are automatically removed from the attribute bag, without needing to redefine them in `@props` +- Adds support for passing attributes via. the `` attribute ## [v1.0.4](https://github.com/Stillat/dagger/compare/v1.0.3...v1.0.4) - 2025-01-21 diff --git a/src/Compiler/AttributeCompiler.php b/src/Compiler/AttributeCompiler.php index 1225b8a..c10f77e 100644 --- a/src/Compiler/AttributeCompiler.php +++ b/src/Compiler/AttributeCompiler.php @@ -88,6 +88,26 @@ public function compileValue(ParameterNode $parameter): string return $parameter->valueNode->content; } + protected function transformParameters(array $parameters): array + { + $newParams = []; + + foreach ($parameters as $parameter) { + if ($parameter->type == ParameterType::AttributeEcho) { + $newParams[] = ParameterFactory::makeVariableReference( + 'attributes', + (string) str($parameter->content)->trim()->substr(2, -2)->trim() + ); + + continue; + } + + $newParams[] = $parameter; + } + + return $newParams; + } + /** * @param ParameterNode[] $parameters */ @@ -99,6 +119,8 @@ public function toCompiledArray(array $parameters, array $propNames = []): array return []; } + $parameters = $this->transformParameters($parameters); + $compiledParameters = []; foreach ($parameters as $parameter) { diff --git a/src/Compiler/ParameterFactory.php b/src/Compiler/ParameterFactory.php new file mode 100644 index 0000000..fecaaeb --- /dev/null +++ b/src/Compiler/ParameterFactory.php @@ -0,0 +1,19 @@ +type = ParameterType::DynamicVariable; + $param->value = $value; + $param->name = $param->materializedName = $variableName; + + return $param; + } +} diff --git a/tests/Compiler/AttributesTest.php b/tests/Compiler/AttributesTest.php index 96d56d3..35532ce 100644 --- a/tests/Compiler/AttributesTest.php +++ b/tests/Compiler/AttributesTest.php @@ -75,3 +75,27 @@ $this->render('') ); }); + +test('attribute passing', function () { + $daggerTemplate = <<<'BLADE' + +BLADE; + + $bladeTemplate = <<<'BLADE' + +BLADE; + + $expected = <<<'EXPECTED' +Root: Child: data-thing="value" class="mt-4" data-foo="bar"After Child: class="mt-4" data-foo="bar" +EXPECTED; + + $this->assertSame( + $expected, + $this->render($daggerTemplate) + ); + + $this->assertSame( + $expected, + $this->render($bladeTemplate) + ); +}); diff --git a/tests/resources/components/attribute_passing_child.blade.php b/tests/resources/components/attribute_passing_child.blade.php new file mode 100644 index 0000000..4e7851d --- /dev/null +++ b/tests/resources/components/attribute_passing_child.blade.php @@ -0,0 +1 @@ +Child: {{ $attributes->merge(['data-thing' => 'value']) }} \ No newline at end of file diff --git a/tests/resources/components/attribute_passing_root.blade.php b/tests/resources/components/attribute_passing_root.blade.php new file mode 100644 index 0000000..a81008f --- /dev/null +++ b/tests/resources/components/attribute_passing_root.blade.php @@ -0,0 +1,2 @@ +Root: +After Child: {{ $attributes }} \ No newline at end of file diff --git a/tests/resources/views/components/attribute_passing_child.blade.php b/tests/resources/views/components/attribute_passing_child.blade.php new file mode 100644 index 0000000..4e7851d --- /dev/null +++ b/tests/resources/views/components/attribute_passing_child.blade.php @@ -0,0 +1 @@ +Child: {{ $attributes->merge(['data-thing' => 'value']) }} \ No newline at end of file diff --git a/tests/resources/views/components/attribute_passing_root.blade.php b/tests/resources/views/components/attribute_passing_root.blade.php new file mode 100644 index 0000000..bea283d --- /dev/null +++ b/tests/resources/views/components/attribute_passing_root.blade.php @@ -0,0 +1,2 @@ +Root: +After Child: {{ $attributes }} \ No newline at end of file