Skip to content

Commit c3569da

Browse files
committed
[TwigComponent] make public component properties available directly
1 parent 6c39a68 commit c3569da

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

src/TwigComponent/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
# 2.1
4+
5+
- Make public component properties available directly in the template (`{{ prop }}` vs `{{ this.prop }}`).
6+
37
## 2.0.0
48

59
- Support for `stimulus` version 2 was removed and support for `@hotwired/stimulus`

src/TwigComponent/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ And (2) a corresponding template:
2727

2828
```twig
2929
{# templates/components/alert.html.twig #}
30-
<div class="alert alert-{{ this.type }}">
31-
{{ this.message }}
30+
<div class="alert alert-{{ type }}">
31+
{{ message }}
3232
</div>
3333
```
3434

@@ -128,11 +128,12 @@ class AlertComponent
128128
```
129129

130130
In the template, the `AlertComponent` instance is available via
131-
the `this` variable. Use it to render the two new properties:
131+
the `this` variable and public properties are available directly.
132+
Use them to render the two new properties:
132133

133134
```twig
134-
<div class="alert alert-{{ this.type }}">
135-
{{ this.message }}
135+
<div class="alert alert-{{ type }}">
136+
{{ message }}
136137
</div>
137138
```
138139

src/TwigComponent/src/ComponentRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ public function __construct(Environment $twig)
3030
public function render(object $component, string $template): string
3131
{
3232
// TODO: Self-Rendering components?
33-
return $this->twig->render($template, ['this' => $component]);
33+
return $this->twig->render($template, array_merge(['this' => $component], get_object_vars($component)));
3434
}
3535
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
propA: {{ this.propA }}
1+
propA: {{ propA }}
22
propB: {{ this.propB }}
33
service: {{ this.service.value }}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Custom template 1
2-
b value: {{ this.value }}
2+
b value: {{ value }}

0 commit comments

Comments
 (0)