Skip to content

Commit 964b9bd

Browse files
committed
[TwigComponent] make public component properties available directly
1 parent 22aca54 commit 964b9bd

File tree

6 files changed

+34
-26
lines changed

6 files changed

+34
-26
lines changed

src/LiveComponent/src/Resources/doc/index.rst

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ A real-time product search component might look like this::
4545
<input
4646
type="search"
4747
name="query"
48-
value="{{ this.query }}"
48+
value="{{ query }}"
4949
data-action="live#update"
5050
>
5151
@@ -247,19 +247,19 @@ Let's add two inputs to our template:
247247
<div {{ init_live_component(this) }}>
248248
<input
249249
type="number"
250-
value="{{ this.min }}"
250+
value="{{ min }}"
251251
data-model="min"
252252
data-action="live#update"
253253
>
254254
255255
<input
256256
type="number"
257-
value="{{ this.max }}"
257+
value="{{ max }}"
258258
data-model="max"
259259
data-action="live#update"
260260
>
261261
262-
Generating a number between {{ this.min }} and {{ this.max }}
262+
Generating a number between {{ min }} and {{ max }}
263263
<strong>{{ this.randomNumber }}</strong>
264264
</div>
265265
@@ -316,7 +316,7 @@ happens, add it to the ``data-action`` call:
316316
317317
<input
318318
type="number"
319-
value="{{ this.max }}"
319+
value="{{ max }}"
320320
data-model="max"
321321
- data-action="live#update"
322322
+ data-action="change->live#update"
@@ -341,7 +341,7 @@ clicked). To do that, use the ``updateDefer`` method:
341341
342342
<input
343343
type="number"
344-
value="{{ this.max }}"
344+
value="{{ max }}"
345345
data-model="max"
346346
- data-action="live#update"
347347
+ data-action="live#updateDefer"
@@ -364,7 +364,7 @@ property. The following code works identically to the previous example:
364364
<div {{ init_live_component(this)>
365365
<input
366366
type="number"
367-
value="{{ this.min }}"
367+
value="{{ min }}"
368368
- data-model="min"
369369
+ name="min"
370370
data-action="live#update"
@@ -1016,19 +1016,19 @@ rendered the ``content`` through a Markdown filter from the
10161016
<div {{init_live_component(this)}}>
10171017
<input
10181018
type="text"
1019-
value="{{ this.post.title }}"
1019+
value="{{ post.title }}"
10201020
data-model="post.title"
10211021
data-action="live#update"
10221022
>
10231023
10241024
<textarea
10251025
data-model="post.content"
10261026
data-action="live#update"
1027-
>{{this.post.content}}</textarea>
1027+
>{{post.content}}</textarea>
10281028
10291029
<div className="markdown-preview" data-loading="addClass(low-opacity)">
1030-
<h3>{{this.post.title}}</h3>
1031-
{{this.post.content | markdown_to_html}}
1030+
<h3>{{post.title}}</h3>
1031+
{{post.content | markdown_to_html}}
10321032
</div>
10331033
</div>
10341034
@@ -1142,7 +1142,7 @@ method:
11421142
data-model="post.content"
11431143
data-action="live#update"
11441144
class="{{ this.getError('post.content') ? 'has-error' : '' }}"
1145-
>{{ this.post.content }}</textarea>
1145+
>{{ post.content }}</textarea>
11461146
11471147
Once a component has been validated, the component will “rememeber” that
11481148
it has been validated. This means that, if you edit a field and the
@@ -1162,7 +1162,7 @@ blur the field), update the model via the ``change`` event:
11621162
data-model="post.content"
11631163
data-action="change->live#update"
11641164
class="{{ this.getError('post.content') ? 'has-error' : '' }}"
1165-
>{{ this.post.content }}</textarea>
1165+
>{{ post.content }}</textarea>
11661166
11671167
When the component re-renders, it will signal to the server that this
11681168
one field should be validated. Like with normal validation, once an
@@ -1399,13 +1399,13 @@ In the ``EditPostComponent`` template, you render the
13991399
type="text"
14001400
name="post[title]"
14011401
data-action="live#update"
1402-
value="{{ this.post.title }}"
1402+
value="{{ post.title }}"
14031403
>
14041404
14051405
{{ component('markdown_textarea', {
14061406
name: 'post[content]',
14071407
label: 'Content',
1408-
value: this.post.content
1408+
value: post.content
14091409
}) }}
14101410
14111411
<button
@@ -1418,13 +1418,13 @@ In the ``EditPostComponent`` template, you render the
14181418
14191419
<div {{ init_live_component(this) }} class="mb-3">
14201420
<textarea
1421-
name="{{ this.name }}"
1421+
name="{{ name }}"
14221422
data-model="value"
14231423
data-action="live#update"
1424-
>{{ this.value }}</textarea>
1424+
>{{ value }}</textarea>
14251425
14261426
<div class="markdown-preview">
1427-
{{ this.value|markdown_to_html }}
1427+
{{ value|markdown_to_html }}
14281428
</div>
14291429
</div>
14301430

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/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
}

src/TwigComponent/src/Resources/doc/index.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ And (2) a corresponding template:
2727
.. code-block:: twig
2828
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
Done! Now render it wherever you want:
@@ -130,12 +130,16 @@ public property for each:
130130
// ...
131131
}
132132
133-
In the template, the ``AlertComponent`` instance is available via the
134-
``this`` variable. Use it to render the two new properties:
133+
In the template, the ``AlertComponent`` instance is available via
134+
the ``this`` variable and public properties are available directly.
135+
Use them to render the two new properties:
135136

136137
.. code-block:: twig
137138
138-
<div class="alert alert-{{ this.type }}">
139+
<div class="alert alert-{{ type }}">
140+
{{ message }}
141+
142+
{# Same as above, but using "this", which is the component object #}
139143
{{ this.message }}
140144
</div>
141145
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)