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
+55-55
Original file line number
Diff line number
Diff line change
@@ -1,28 +1,28 @@
1
-
# Blade Templates
1
+
# 模板Blade
2
2
3
-
-[Introduction](#introduction)
4
-
-[Template Inheritance](#template-inheritance)
5
-
-[Defining A Layout](#defining-a-layout)
6
-
-[Extending A Layout](#extending-a-layout)
7
-
-[Displaying Data](#displaying-data)
8
-
-[Control Structures](#control-structures)
9
-
-[Service Injection](#service-injection)
10
-
-[Extending Blade](#extending-blade)
3
+
-[简介](#introduction)
4
+
-[模板继承](#template-inheritance)
5
+
-[定义布局](#defining-a-layout)
6
+
-[扩展布局](#extending-a-layout)
7
+
-[显示数据](#displaying-data)
8
+
-[控制器](#control-structures)
9
+
-[服务依赖](#service-injection)
10
+
-[Blade扩展](#extending-blade)
11
11
12
12
<aname="introduction"></a>
13
-
## Introduction
13
+
## 简介
14
14
15
-
Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. All Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the `.blade.php` file extension and are typically stored in the `resources/views` directory.
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:
@@ -39,16 +39,16 @@ Two of the primary benefits of using Blade are _template inheritance_ and _secti
39
39
</body>
40
40
</html>
41
41
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.
Now that we have defined a layout for our application, let's define a child page that inherits the layout.
44
+
现在我们已经定义了应用的布局,接下来我们对这个布局继承一个子页面。
45
45
46
46
<aname="extending-a-layout"></a>
47
-
### Extending A Layout
47
+
### 布局扩展
48
48
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`:
@@ -64,71 +64,71 @@ When defining a child page, you may use the Blade `@extends` directive to specif
64
64
<p>This is my body content.</p>
65
65
@endsection
66
66
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.
Of course, just like plain PHP views, Blade views may be returned from routes using the global `view` helper function:
69
+
当然,这些简单的PHP视图,路由会调用`view`帮助方法将这些Blade视图返回。
70
70
71
71
Route::get('blade', function () {
72
72
return view('child');
73
73
});
74
74
75
75
<aname="displaying-data"></a>
76
-
## Displaying Data
76
+
## 展示数据
77
77
78
-
You may display data passed to your Blade views by wrapping the variable in "curly" braces. For example, given the following route:
78
+
在Blade视图中展示数据时,需要将变量放在大括号中括起来。请看下面的路由的例子:
79
79
80
80
Route::get('greeting', function () {
81
81
return view('welcome', ['name' => 'Samantha']);
82
82
});
83
83
84
-
You may display the contents of the `name` variable like so:
84
+
如果想展示`name`的内容,可以用下面的方法:
85
85
86
86
Hello, {{ $name }}.
87
87
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:
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:
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.
However, instead of writing a ternary statement, Blade provides you with the following convenient short-cut:
110
+
高兴的是,Blade提供了一个简单快捷的三元运算表示方法。
111
111
112
112
{{ $name or 'Default' }}
113
113
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.
114
+
这个例子中,`$name`如果存在,就会被展示。如果不存在,就会输出一个默认的`Default`。
115
115
116
-
#### Displaying Unescaped Data
116
+
#### 展示非转义的数据
117
117
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:
> **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.
In addition to template inheritance and displaying data, Blade also provides convenient short-cuts for common PHP control structures, such as conditional statements and loops. These short-cuts provide a very clean, terse way of working with PHP control structures, while also remaining familiar to their PHP counterparts.
You may construct `if` statements using the `@if`,`@elseif`,`@else`, and `@endif` directives. These directives function identically to their PHP counterparts:
@@ -138,15 +138,15 @@ You may construct `if` statements using the `@if`, `@elseif`, `@else`, and `@end
138
138
I don't have any records!
139
139
@endif
140
140
141
-
For convenience, Blade also provides an `@unless` directive:
141
+
为了更方便,Blade还提供了一个`@unless`命令:
142
142
143
143
@unless (Auth::check())
144
144
You are not signed in.
145
145
@endunless
146
146
147
-
#### Loops
147
+
#### 循环
148
148
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:
@@ -166,9 +166,9 @@ In addition to conditional statements, Blade provides simple directives for work
166
166
<p>I'm looping forever.</p>
167
167
@endwhile
168
168
169
-
#### Including Sub-Views
169
+
#### 包含子视图
170
170
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:
@@ -178,20 +178,20 @@ Blade's `@include` directive, allows you to easily include a Blade view from wit
178
178
</form>
179
179
</div>
180
180
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:
181
+
尽管包含的视图可以继承父级视图的所有数据变量,你也仍然可以在包含的视图中对数据进行扩展:
182
182
183
183
@include('view.name', ['some' => 'data'])
184
184
185
-
#### Comments
185
+
#### 注释
186
186
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:
187
+
Blade 也允许你在视图中注释。但是你的应用程序只能包含HTML注释:
188
188
189
189
{{-- This comment will not be present in the rendered HTML --}}
190
190
191
191
<aname="service-injection"></a>
192
-
## Service Injection
192
+
## 服务注入
193
193
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:
@@ -200,11 +200,11 @@ The `@inject` directive may be used to retrieve a service from the Laravel [serv
200
200
</div>
201
201
202
202
<aname="extending-blade"></a>
203
-
## Extending Blade
203
+
## Blade 扩展
204
204
205
-
Blade even allows you to define your own custom directives. You can use the `directive` method to register a directive. When the Blade compiler encounters the directive, it calls the provided callback with its parameter.
The following example creates a `@datetime($var)` directive which formats a given `$var`:
207
+
下面的例子就是创建了一个`@datetime($var)`的命令来格式化给定的`$var`:
208
208
209
209
<?php
210
210
@@ -238,7 +238,7 @@ The following example creates a `@datetime($var)` directive which formats a give
238
238
}
239
239
}
240
240
241
-
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