Skip to content

Commit ec46dfb

Browse files
committed
Merge pull request #46 from newworldba/5.1
blade.md
2 parents 6b813ed + f0c1209 commit ec46dfb

File tree

1 file changed

+55
-55
lines changed

1 file changed

+55
-55
lines changed

blade.md

+55-55
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
# Blade Templates
1+
# 模板Blade
22

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)
1111

1212
<a name="introduction"></a>
13-
## Introduction
13+
## 简介
1414

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.
15+
Laravel提供了一个简单强大的模板引擎. 与其他的PHP模板引擎不一样的是,Blade在Views中允许使用PHP代码. Blade视图发生变化时,就会被解析成PHP代码,并且缓存起来,所以Blade不会对你的应用程序产生任何负担。 Blade视图文件以‘blade.php’结尾,所有文件都放在'resources/views'文件夹中。
1616

1717
<a name="template-inheritance"></a>
18-
## Template Inheritance
18+
## 模板继承
1919

2020
<a name="defining-a-layout"></a>
21-
### Defining A Layout
21+
### 定义布局
2222

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:
23+
Blade两个主要的优点就是_模板继承_和_模块化_.在这之前,先看一个简单的例子。首先,我们先看一个“master”布局页面。由于大多数的web应用都使用相同的布局,所以我们只需要定义一种Blade视图。
2424

25-
<!-- Stored in resources/views/layouts/master.blade.php -->
25+
<!-- 文件位置 resources/views/layouts/master.blade.php -->
2626

2727
<html>
2828
<head>
@@ -39,16 +39,16 @@ Two of the primary benefits of using Blade are _template inheritance_ and _secti
3939
</body>
4040
</html>
4141

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.
42+
这个文件是非常经典的HTTML标记。需要注意的是,这里面有`@section``@yield`命令。`@section`和字面上的意思一样,它定义了内容的一部分,而`@yield`命令用来表示给出的section的内容。
4343

44-
Now that we have defined a layout for our application, let's define a child page that inherits the layout.
44+
现在我们已经定义了应用的布局,接下来我们对这个布局继承一个子页面。
4545

4646
<a name="extending-a-layout"></a>
47-
### Extending A Layout
47+
### 布局扩展
4848

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`:
49+
当定义子页面时,我们就可以用Blade的`@extends`命令来说明这个子页面'inherit'(继承)的是哪个。`@extends`一个Blade布局的视图可以将内容注入到用`@section`命令的布局中的某一个部分。记住,在上面的这个例子中,我们是用`@yield`命令来展示布局中的这些部分内容。
5050

51-
<!-- Stored in resources/views/layouts/child.blade.php -->
51+
<!-- 代码路径 resources/views/layouts/child.blade.php -->
5252

5353
@extends('layouts.master')
5454

@@ -64,71 +64,71 @@ When defining a child page, you may use the Blade `@extends` directive to specif
6464
<p>This is my body content.</p>
6565
@endsection
6666

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.
67+
在这个例子中,'sidebar'部分就是利用`@@parent`命令来将内容追加到布局中的sidebar中。这个视图被渲染时,`@@parent`命令就会被布局中的内容所替换。
6868

69-
Of course, just like plain PHP views, Blade views may be returned from routes using the global `view` helper function:
69+
当然,这些简单的PHP视图,路由会调用`view`帮助方法将这些Blade视图返回。
7070

7171
Route::get('blade', function () {
7272
return view('child');
7373
});
7474

7575
<a name="displaying-data"></a>
76-
## Displaying Data
76+
## 展示数据
7777

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视图中展示数据时,需要将变量放在大括号中括起来。请看下面的路由的例子:
7979

8080
Route::get('greeting', function () {
8181
return view('welcome', ['name' => 'Samantha']);
8282
});
8383

84-
You may display the contents of the `name` variable like so:
84+
如果想展示`name`的内容,可以用下面的方法:
8585

8686
Hello, {{ $name }}.
8787

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:
88+
当然,你不会被局限于使用视图中的变量,你也可以用PHP方法输出结果。事实上,你在Blade中可以任意使用PHP代码来输出语句:
8989

9090
The current UNIX timestamp is {{ time() }}.
9191

92-
> **Note:** Blade `{{ }}` statements are automatically sent through PHP's `htmlentities` function to prevent XSS attacks.
92+
> **Note:** Blade 中的`{{}}`语句会自动调用PHP的`htmlentities`方法来抵御XSS攻击。
9393
94-
#### Blade & JavaScript Frameworks
94+
#### Blade & JavaScript 框架
9595

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:
96+
由于JavaScript框架在浏览器中大都使用大括号来表示表达式,你可以使用`@`符号来告诉Blade引擎这个表达式不需要解析。例如:
9797

9898
<h1>Laravel</h1>
9999

100100
Hello, @{{ name }}.
101101

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.
102+
这个例子中,这个`@`符号在Blade中会被删掉;然而,`{{name}}`表达式在Blade引擎中会保持不变,这样JavaScript框架就会对它进行处理。
103103

104-
#### Echoing Data If It Exists
104+
#### 三元运算
105105

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:
106+
有的时候你想输入一个变量时,但是你可能不确定这个变量是否被定义了。在PHP代码中,我们就需要这么写,但是这样有点啰嗦:
107107

108108
{{ isset($name) ? $name : 'Default' }}
109109

110-
However, instead of writing a ternary statement, Blade provides you with the following convenient short-cut:
110+
高兴的是,Blade提供了一个简单快捷的三元运算表示方法。
111111

112112
{{ $name or 'Default' }}
113113

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`
115115

116-
#### Displaying Unescaped Data
116+
#### 展示非转义的数据
117117

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:
118+
默认情况下,Blade 中的`{{ }}`语句会自动调用PHP中的`htmlentities`方法转义来抵御XSS攻击。如果你不想让数据被转义,可以用下面的语法:
119119

120120
Hello, {!! $name !!}.
121121

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.
122+
> **Note:** 在应用程序中输出用户的输入的数据要非常小心。我们经常会用双花括号表示我们需要转义成HTML实体。
123123
124124
<a name="control-structures"></a>
125-
## Control Structures
125+
## 控制结构
126126

127-
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.
127+
除了模板集成和展示动态数据,Blade也提供了简单的PHP结构控制结构,比如条件语句和循环语句。这些短标签和PHP控制语句一起使用,非常简洁优雅,对PHP非常友好。
128128

129-
#### If Statements
129+
#### 条件语句
130130

131-
You may construct `if` statements using the `@if`, `@elseif`, `@else`, and `@endif` directives. These directives function identically to their PHP counterparts:
131+
条件语句中可以使用`@if`,`@elseif`,`@endif`命令,这些命令跟PHP的使用方式一样:
132132

133133
@if (count($records) === 1)
134134
I have one record!
@@ -138,15 +138,15 @@ You may construct `if` statements using the `@if`, `@elseif`, `@else`, and `@end
138138
I don't have any records!
139139
@endif
140140

141-
For convenience, Blade also provides an `@unless` directive:
141+
为了更方便,Blade还提供了一个`@unless`命令:
142142

143143
@unless (Auth::check())
144144
You are not signed in.
145145
@endunless
146146

147-
#### Loops
147+
#### 循环
148148

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:
149+
除了条件语句,Blade还提供了一个简单的命令来对应PHP中的循环结构。他们跟PHP的用法是一样的:
150150

151151
@for ($i = 0; $i < 10; $i++)
152152
The current value is {{ $i }}
@@ -166,9 +166,9 @@ In addition to conditional statements, Blade provides simple directives for work
166166
<p>I'm looping forever.</p>
167167
@endwhile
168168

169-
#### Including Sub-Views
169+
#### 包含子视图
170170

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:
171+
Blade中的`@include`命令,让你在一个已有的视图里面包含一个Blade视图变得更加容易。被包含的视图可以使用所有的父级视图中的变量:
172172

173173
<div>
174174
@include('shared.errors')
@@ -178,20 +178,20 @@ Blade's `@include` directive, allows you to easily include a Blade view from wit
178178
</form>
179179
</div>
180180

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+
尽管包含的视图可以继承父级视图的所有数据变量,你也仍然可以在包含的视图中对数据进行扩展:
182182

183183
@include('view.name', ['some' => 'data'])
184184

185-
#### Comments
185+
#### 注释
186186

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注释:
188188

189189
{{-- This comment will not be present in the rendered HTML --}}
190190

191191
<a name="service-injection"></a>
192-
## Service Injection
192+
## 服务注入
193193

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:
194+
`@inject` 可以将Laravel中的服务[服务容器](/docs/{{version}}/container)注入进来.`@inject`的第一个参数将会被定义成service的名称,第二个参数是用来被处理的class/interface:
195195

196196
@inject('metrics', 'App\Services\MetricsService')
197197

@@ -200,11 +200,11 @@ The `@inject` directive may be used to retrieve a service from the Laravel [serv
200200
</div>
201201

202202
<a name="extending-blade"></a>
203-
## Extending Blade
203+
## Blade 扩展
204204

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.
205+
Blade甚至允许你定义你的习惯用法。你可以使用`directive`方式来注册一个命令。当Blade编译器遇到这个命令时,他就会调用回调方法来处理这些参数。
206206

207-
The following example creates a `@datetime($var)` directive which formats a given `$var`:
207+
下面的例子就是创建了一个`@datetime($var)`的命令来格式化给定的`$var`
208208

209209
<?php
210210

@@ -238,7 +238,7 @@ The following example creates a `@datetime($var)` directive which formats a give
238238
}
239239
}
240240

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:
241+
你可以看到,Laravel中的`with`帮助方法在下面的语句中会被使用。这个`with`方法用链式方式简单的返回一个object/value,最终的PHP语句是这样的:
242242

243243
<?php echo with($var)->format('m/d/Y H:i'); ?>
244244

0 commit comments

Comments
 (0)