Skip to content

Commit 96f3149

Browse files
erikaraujobrendt
andauthored
feat(view): attribute precedence (#1153)
Co-authored-by: brendt <[email protected]>
1 parent 0c553d4 commit 96f3149

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/Tempest/View/src/Elements/IsElement.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,23 @@ public function getAttributes(): array
3232
$wrappingAttributes = [];
3333
}
3434

35-
return [...$this->attributes, ...$wrappingAttributes];
35+
$attributes = [...$this->attributes, ...$wrappingAttributes];
36+
37+
// Some attributes should always come after others,
38+
// so that these expressions have access to the data attributes
39+
$attributePrecedence = [
40+
':foreach' => 1,
41+
':if' => 2,
42+
];
43+
44+
uksort($attributes, function (string $a, string $b) use ($attributePrecedence) {
45+
$precedenceA = $attributePrecedence[$a] ?? 0;
46+
$precedenceB = $attributePrecedence[$b] ?? 0;
47+
48+
return $precedenceA <=> $precedenceB;
49+
});
50+
51+
return $attributes;
3652
}
3753

3854
public function hasAttribute(string $name): bool

tests/Integration/View/TempestViewRendererTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,35 @@ public function test_with_colon_symbol_in_html_tag(): void
575575
);
576576
}
577577

578+
public function test_loop_variable_can_be_used_within_the_looped_tag(): void
579+
{
580+
$html = $this->render(
581+
view(
582+
<<<'HTML'
583+
<a :foreach="$items as $item" :href="$item->uri">
584+
{{ $item->title }}
585+
</a>
586+
HTML,
587+
)
588+
->data(items: [
589+
new class {
590+
public string $title = 'Item 1';
591+
592+
public string $uri = '/item-1';
593+
},
594+
new class {
595+
public string $title = 'Item 2';
596+
597+
public string $uri = '/item-2';
598+
},
599+
]),
600+
);
601+
602+
$this->assertSnippetsMatch(<<<'HTML'
603+
<a href="/item-1">Item 1</a><a href="/item-2">Item 2</a>
604+
HTML, $html);
605+
}
606+
578607
private function assertSnippetsMatch(string $expected, string $actual): void
579608
{
580609
$expected = str_replace([PHP_EOL, ' '], '', $expected);

0 commit comments

Comments
 (0)