forked from livewire/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rendering-components.blade.php
executable file
·377 lines (293 loc) · 10.2 KB
/
rendering-components.blade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
* [Inline Components](#inline-components) { .text-blue-800 }
* [Parameters](#parameters) { .font-normal.text-sm.text-blue-800 }
* [Full-Page Components](#page-components) { .text-blue-800 }
* [Configuring The Layout Component](#custom-layout) { .font-normal.text-sm.text-blue-800 }
* [Route Parameters](#route-params) { .font-normal.text-sm.text-blue-800 }
* [Route Model Binding](#route-model-binding) { .font-normal.text-sm.text-blue-800 }
* [The Render Method](#render-method) { .text-blue-800 }
* [Returning Blade Views](#returning-blade) { .font-normal.text-sm.text-blue-800 }
* [Returning Template String](#returning-strings) { .font-normal.text-sm.text-blue-800 }
<div> </div>
@include('includes.screencast-cta')
## Inline Components {#inline-components}
The most basic way to render a Livewire component on a page is using the `<livewire:` tag syntax:
@component('components.code', ['lang' => 'blade'])
@verbatim
<div>
<livewire:show-posts />
</div>
@endverbatim
@endcomponent
Alternatively you can use the `@@livewire` blade directive:
@component('components.code', ['lang' => 'blade'])
@verbatim
@livewire('show-posts')
@endverbatim
@endcomponent
If you have a component inside of a sub-folder with its own namespace, you must use a dot (`.`) prefixed with the namespace.
For example, if we have a `ShowPosts` component inside of a `app/Http/Livewire/Nav` folder, we would indicate it as such:
@component('components.code', ['lang' => 'blade'])
@verbatim
<livewire:nav.show-posts />
@endverbatim
@endcomponent
### Parameters {#parameters}
#### Passing Parameters
You can pass data into a component by passing additional parameters into the <code><livewire:</code> tag.
For example, let's say we have a `show-post` component. Here's how you would pass in a `$post` model.
@component('components.code', ['lang' => 'blade'])
@verbatim
<livewire:show-post :post="$post">
@endverbatim
@endcomponent
Alternatively, this is how you can pass in parameters using the Blade directive.
@component('components.code', ['lang' => 'blade'])
@verbatim
@livewire('show-post', ['post' => $post])
@endverbatim
@endcomponent
#### Receiving Parameters
Livewire will automatically assign parameters to matching public properties.
For example, in the case of @verbatim `<livewire:show-post :post="$post">` @endverbatim, if the `show-post` component has a public property named `$post`, it will be automatically assigned:
@component('components.code', ['lang' => 'php'])
@verbatim
class ShowPost extends Component
{
public $post;
...
}
@endverbatim
@endcomponent
If for whatever reason, this automatic behavior doesn't work well for you, you can intercept parameters using the `mount()` method:
@component('components.code', ['lang' => 'php'])
@verbatim
class ShowPost extends Component
{
public $title;
public $content;
public function mount($post)
{
$this->title = $post->title;
$this->content = $post->content;
}
...
}
@endverbatim
@endcomponent
@component('components.tip')
In Livewire components, you use <code>mount()</code> instead of a class constructor <code>__construct()</code> like you may be used to.
@endcomponent
Like a controller, you can inject dependencies by adding type-hinted parameters before passed-in ones.
@component('components.code', ['lang' => 'php'])
@verbatim
use \Illuminate\Session\SessionManager;
class ShowPost extends Component
{
public $title;
public $content;
public function mount(SessionManager $session, $post)
{
$session->put("post.{$post->id}.last_viewed", now());
$this->title = $post->title;
$this->content = $post->content;
}
...
}
@endverbatim
@endcomponent
## Full-Page Components {#page-components}
If the main content of a page is a Livewire component, you can pass the component directly into a Laravel route as if it were a controller.
@component('components.code', ['lang' => 'php'])
@verbatim
Route::get('/post', ShowPosts::class);
@endverbatim
@endcomponent
By default, Livewire will render the `ShowPosts` component into the `@{{ $slot }}` of a blade layout component located at: `resources/views/layouts/app.blade.php`
@component('components.code-component')
@slot('view')
@verbatim
<head>
@livewireStyles
</head>
<body>
{{ $slot }}
@livewireScripts
</body>
@endverbatim
@endslot
@endcomponent
For more information on Laravel components, [visit Laravel's documentation](https://laravel.com/docs/blade#components).
### Configuring The Layout Component {#custom-layout}
If you want to specify a default layout other than the `layouts.app`, you can override the `livewire.layout` config option.
@component('components.code', ['lang' => 'php'])
'layout' => 'app.other_default_layout'
@endcomponent
If you need even more control, you can use the `->layout()` method on the view instance you return from `render()`.
@component('components.code', ['lang' => 'php'])
@verbatim
class ShowPosts extends Component
{
...
public function render()
{
return view('livewire.show-posts')
->layout('layouts.base');
}
}
@endverbatim
@endcomponent
If you are using a non-default slot in the component, you can also chain on `->slot()`:
@component('components.code', ['lang' => 'php'])
public function render()
{
return view('livewire.show-posts')
->layout('layouts.base')
->slot('main');
}
@endcomponent
Alternatively, Livewire supports using traditional Blade layout files with `@@extends`.
Given the following layout file:
@component('components.code')
@verbatim
<head>
@livewireStyles
</head>
<body>
@yield('content')
@livewireScripts
</body>
@endverbatim
@endcomponent
You can configure Livewire to reference it using `->extends()` instead of `->layout()`:
@component('components.code', ['lang' => 'php'])
public function render()
{
return view('livewire.show-posts')
->extends('layouts.app');
}
@endcomponent
If you need to configure the `@@section` for the component to use, you can configure that as well with the `->section()` method:
@component('components.code', ['lang' => 'php'])
public function render()
{
return view('livewire.show-posts')
->extends('layouts.app')
->section('body');
}
@endcomponent
If you need to pass data from your components to your layout, you can pass the data along with the layout method:
@component('components.code', ['lang' => 'php'])
public function render()
{
return view('livewire.show-posts')
->layout('layouts.base', ['title' => 'Show Posts'])
}
@endcomponent
In some cases you don't need to pass your layout name or you want to pass layout data separately, you can use layoutData method:
@component('components.code', ['lang' => 'php'])
public function render()
{
return view('livewire.show-posts')
->layoutData(['title' => 'Show Posts'])
}
@endcomponent
### Route Parameters {#route-params}
Often you need to access route parameters inside your controller methods. Because we are no longer using controllers, Livewire attempts to mimic this behavior through its `mount` method. For example:
@component('components.code', ['lang' => 'php'])
Route::get('/post/{id}', ShowPost::class);
@endcomponent
@component('components.code', ['lang' => 'php'])
class ShowPost extends Component
{
public $post;
public function mount($id)
{
$this->post = Post::find($id);
}
...
}
@endcomponent
As you can see, the `mount` method in a Livewire component is acting like a controller method would as far as its parameters go. If you visit `/post/123`, the `$id` variable passed into the `mount` method will contain the value `123`.
### Route Model Binding {#route-model-binding}
Like you would expect, Livewire components implement all functionality you're used to in your controllers including route model binding. For example:
@component('components.code', ['lang' => 'php'])
Route::get('/post/{post}', ShowPost::class);
@endcomponent
@component('components.code', ['lang' => 'php'])
class ShowPost extends Component
{
public $post;
public function mount(Post $post)
{
$this->post = $post;
}
}
@endcomponent
If you are using PHP 7.4, you can also typehint class properties, and Livewire will automatically route-model bind to them. The following component's `$post` property will be automatically injected with no need for the `mount()` method.
@component('components.code', ['lang' => 'php'])
class ShowPost extends Component
{
public Post $post;
}
@endcomponent
## The Render Method {#render-method}
A Livewire component's `render` method gets called on the initial page load AND every subsequent component update.
@component('components.tip')
In simple components, you don't need to define a `render` method yourself. The base Livewire component class has a dynamic `render` method included.
@endcomponent
### Returning Blade Views {#returning-blade}
The `render()` method is expected to return a Blade view, therefore, you can compare it to writing a controller method. Here is an example:
@component('components.warning')
Make sure your Blade view only has ONE root element.
@endcomponent
@component('components.code-component')
@slot('class')
@verbatim
class ShowPosts extends Component
{
public function render()
{
return view('livewire.show-posts', [
'posts' => Post::all(),
]);
}
}
@endverbatim
@endslot
@slot('view')
@verbatim
<div>
@foreach ($posts as $post)
@include('includes.post', $post)
@endforeach
</div>
@endverbatim
@endslot
@endcomponent
### Returning Template Strings {#returning-strings}
In addition to Blade views, you can optionally return a Blade template string from `render()`.
@component('components.code-component')
@slot('class')
@verbatim
class DeletePost extends Component
{
public Post $post;
public function delete()
{
$this->post->delete();
}
public function render()
{
return <<<'blade'
<div>
<button wire:click="delete">Delete Post</button>
</div>
blade;
}
}
@endverbatim
@endslot
@endcomponent
@component('components.tip')
For inline components like above, you should use the <code>--inline</code> flag during creation: <code>artisan make:livewire delete-post --inline</code>
@endcomponent