You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: blade.md
+107-107
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,8 @@
2
2
3
3
-[Introduction](#introduction)
4
4
-[Template Inheritance](#template-inheritance)
5
-
- [Defining A Layout](#defining-a-layout)
6
-
- [Extending A Layout](#extending-a-layout)
5
+
-[Defining A Layout](#defining-a-layout)
6
+
-[Extending A Layout](#extending-a-layout)
7
7
-[Displaying Data](#displaying-data)
8
8
-[Control Structures](#control-structures)
9
9
-[Service Injection](#service-injection)
@@ -22,22 +22,22 @@ Blade is the simple, yet powerful templating engine provided with Laravel. Unlik
22
22
23
23
Two of the primary benefits of using Blade are _template inheritance_ and _sections_. To get started, let's take a look at a simple example. First, we will examine a "master" page layout. Since most web applications maintain the same general layout across various pages, it's convenient to define this layout as a single Blade view:
24
24
25
-
<!-- Stored in resources/views/layouts/master.blade.php -->
25
+
<!-- Stored in resources/views/layouts/master.blade.php -->
26
26
27
-
<html>
28
-
<head>
29
-
<title>App Name - @yield('title')</title>
30
-
</head>
31
-
<body>
32
-
@section('sidebar')
33
-
This is the master sidebar.
34
-
@show
27
+
<html>
28
+
<head>
29
+
<title>App Name - @yield('title')</title>
30
+
</head>
31
+
<body>
32
+
@section('sidebar')
33
+
This is the master sidebar.
34
+
@show
35
35
36
-
<div class="container">
37
-
@yield('content')
38
-
</div>
39
-
</body>
40
-
</html>
36
+
<div class="container">
37
+
@yield('content')
38
+
</div>
39
+
</body>
40
+
</html>
41
41
42
42
As you can see, this file contains typical HTML mark-up. However, take note of the `@section` and `@yield` directives. The `@section` directive, as the name implies, defines a section of content, while the `@yield` directive is used to display the contents of a given section.
43
43
@@ -48,76 +48,76 @@ Now that we have defined a layout for our application, let's define a child page
48
48
49
49
When defining a child page, you may use the Blade `@extends` directive to specify which layout the child page should "inherit". Views which `@extends` a Blade layout may inject content into the layout's sections using `@section` directives. Remember, as seen in the example above, the contents of these sections will be displayed in the layout using `@yield`:
50
50
51
-
<!-- Stored in resources/views/layouts/child.blade.php -->
51
+
<!-- Stored in resources/views/layouts/child.blade.php -->
52
52
53
-
@extends('layouts.master')
53
+
@extends('layouts.master')
54
54
55
-
@section('title', 'Page Title')
55
+
@section('title', 'Page Title')
56
56
57
-
@section('sidebar')
58
-
@@parent
57
+
@section('sidebar')
58
+
@@parent
59
59
60
-
<p>This is appended to the master sidebar.</p>
61
-
@endsection
60
+
<p>This is appended to the master sidebar.</p>
61
+
@endsection
62
62
63
-
@section('content')
64
-
<p>This is my body content.</p>
65
-
@endsection
63
+
@section('content')
64
+
<p>This is my body content.</p>
65
+
@endsection
66
66
67
67
In this example, the `sidebar` section is utilizing the `@@parent` directive to append (rather than overwriting) content to the layout's sidebar. The `@@parent` directive will be replaced by the content of the layout when the view is rendered.
68
68
69
69
Of course, just like plain PHP views, Blade views may be returned from routes using the global `view` helper function:
70
70
71
-
Route::get('blade', function () {
72
-
return view('child');
73
-
});
71
+
Route::get('blade', function () {
72
+
return view('child');
73
+
});
74
74
75
75
<aname="displaying-data"></a>
76
76
## Displaying Data
77
77
78
78
You may display data passed to your Blade views by wrapping the variable in "curly" braces. For example, given the following route:
79
79
80
-
Route::get('greeting', function () {
81
-
return view('welcome', ['name' => 'Samantha']);
82
-
});
80
+
Route::get('greeting', function () {
81
+
return view('welcome', ['name' => 'Samantha']);
82
+
});
83
83
84
84
You may display the contents of the `name` variable like so:
85
85
86
-
Hello, {{ $name }}.
86
+
Hello, {{ $name }}.
87
87
88
88
Of course, you are not limited to displaying the contents of the variables passed to the view. You may also echo the results of any PHP function. In fact, you can put any PHP code you wish inside of a Blade echo statement:
89
89
90
-
The current UNIX timestamp is {{ time() }}.
90
+
The current UNIX timestamp is {{ time() }}.
91
91
92
92
> **Note:** Blade `{{ }}` statements are automatically sent through PHP's `htmlentities` function to prevent XSS attacks.
93
93
94
94
#### Blade & JavaScript Frameworks
95
95
96
96
Since many JavaScript frameworks also use "curly" braces to indicate a given expression should be displayed in the browser, you may use the `@` symbol to inform the Blade rendering engine an expression should remain untouched. For example:
97
97
98
-
<h1>Laravel</h1>
98
+
<h1>Laravel</h1>
99
99
100
-
Hello, @{{ name }}.
100
+
Hello, @{{ name }}.
101
101
102
102
In this example, the `@` symbol will be removed by Blade; however, `{{ name }}` expression will remain untouched by the Blade engine, allowing it to instead be rendered by your JavaScript framework.
103
103
104
104
#### Echoing Data If It Exists
105
105
106
106
Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. We can express this in verbose PHP code like so:
107
107
108
-
{{ isset($name) ? $name : 'Default' }}
108
+
{{ isset($name) ? $name : 'Default' }}
109
109
110
110
However, instead of writing a ternary statement, Blade provides you with the following convenient short-cut:
111
111
112
-
{{ $name or 'Default' }}
112
+
{{ $name or 'Default' }}
113
113
114
114
In this example, if the `$name` variable exists, its value will be displayed. However, if it does not exist, the word `Default` will be displayed.
115
115
116
116
#### Displaying Unescaped Data
117
117
118
118
By default, Blade `{{ }}` statements are automatically sent through PHP's `htmlentities` function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:
119
119
120
-
Hello, {!! $name !!}.
120
+
Hello, {!! $name !!}.
121
121
122
122
> **Note:** Be very careful when echoing content that is supplied by users of your application. Always use the double curly brace syntax to escape any HTML entities in the content.
123
123
@@ -130,74 +130,74 @@ In addition to template inheritance and displaying data, Blade also provides con
130
130
131
131
You may construct `if` statements using the `@if`, `@elseif`, `@else`, and `@endif` directives. These directives function identically to their PHP counterparts:
132
132
133
-
@if (count($records) === 1)
134
-
I have one record!
135
-
@elseif (count($records) > 1)
136
-
I have multiple records!
137
-
@else
138
-
I don't have any records!
139
-
@endif
133
+
@if (count($records) === 1)
134
+
I have one record!
135
+
@elseif (count($records) > 1)
136
+
I have multiple records!
137
+
@else
138
+
I don't have any records!
139
+
@endif
140
140
141
141
For convenience, Blade also provides an `@unless` directive:
142
142
143
-
@unless (Auth::check())
144
-
You are not signed in.
145
-
@endunless
143
+
@unless (Auth::check())
144
+
You are not signed in.
145
+
@endunless
146
146
147
147
#### Loops
148
148
149
149
In addition to conditional statements, Blade provides simple directives for working with PHP's supported loop structures. Again, each of these directives functions identically to their PHP counterparts:
150
150
151
-
@for ($i = 0; $i < 10; $i++)
152
-
The current value is {{ $i }}
153
-
@endfor
151
+
@for ($i = 0; $i < 10; $i++)
152
+
The current value is {{ $i }}
153
+
@endfor
154
154
155
-
@foreach ($users as $user)
156
-
<p>This is user {{ $user->id }}</p>
157
-
@endforeach
155
+
@foreach ($users as $user)
156
+
<p>This is user {{ $user->id }}</p>
157
+
@endforeach
158
158
159
-
@forelse ($users as $user)
160
-
<li>{{ $user->name }}</li>
161
-
@empty
162
-
<p>No users</p>
163
-
@endforelse
159
+
@forelse ($users as $user)
160
+
<li>{{ $user->name }}</li>
161
+
@empty
162
+
<p>No users</p>
163
+
@endforelse
164
164
165
-
@while (true)
166
-
<p>I'm looping forever.</p>
167
-
@endwhile
165
+
@while (true)
166
+
<p>I'm looping forever.</p>
167
+
@endwhile
168
168
169
169
#### Including Sub-Views
170
170
171
171
Blade's `@include` directive, allows you to easily include a Blade view from within an existing view. All variables that are available to the parent view will be made available to the included view:
172
172
173
-
<div>
174
-
@include('shared.errors')
173
+
<div>
174
+
@include('shared.errors')
175
175
176
-
<form>
177
-
<!-- Form Contents -->
178
-
</form>
179
-
</div>
176
+
<form>
177
+
<!-- Form Contents -->
178
+
</form>
179
+
</div>
180
180
181
181
Even though the included view will inherit all data available in the parent view, you may also pass an array of extra data to the included view:
182
182
183
-
@include('view.name', ['some' => 'data'])
183
+
@include('view.name', ['some' => 'data'])
184
184
185
185
#### Comments
186
186
187
187
Blade also allows you to define comments in your views. However, unlike HTML comments, Blade comments are not included in the HTML returned by your application:
188
188
189
-
{{-- This comment will not be present in the rendered HTML --}}
189
+
{{-- This comment will not be present in the rendered HTML --}}
190
190
191
191
<aname="service-injection"></a>
192
192
## Service Injection
193
193
194
194
The `@inject` directive may be used to retrieve a service from the Laravel [service container](/docs/{{version}}/container). The first argument passed to `@inject` is the name of the variable the service will be placed into, while the second argument is the class / interface name of the service you wish to resolve:
As you can see, Laravel's `with` helper function was used in this directive. The `with` helper simply returns the object / value it is given, allowing for convenient method chaining. The final PHP generated by this directive will be:
0 commit comments