Skip to content

Commit 44cffd4

Browse files
author
weihailong
committed
翻译blade.md
1 parent 6b813ed commit 44cffd4

File tree

1 file changed

+55
-54
lines changed

1 file changed

+55
-54
lines changed

blade.md

+55-54
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,17 @@ 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

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

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

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

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

5354
@extends('layouts.master')
5455

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

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

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

7172
Route::get('blade', function () {
7273
return view('child');
7374
});
7475

7576
<a name="displaying-data"></a>
76-
## Displaying Data
77+
## 展示数据
7778

78-
You may display data passed to your Blade views by wrapping the variable in "curly" braces. For example, given the following route:
79+
在Blade视图中展示数据时,需要将变量放在大括号中括起来。请看下下面的路由的例子:
7980

8081
Route::get('greeting', function () {
8182
return view('welcome', ['name' => 'Samantha']);
8283
});
8384

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

8687
Hello, {{ $name }}.
8788

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

9091
The current UNIX timestamp is {{ time() }}.
9192

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

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

9899
<h1>Laravel</h1>
99100

100101
Hello, @{{ name }}.
101102

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

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

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

108109
{{ isset($name) ? $name : 'Default' }}
109110

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

112113
{{ $name or 'Default' }}
113114

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+
这个例子中,`$name`如果存在,就会被展示。如果不存在,就会输出一个默认的`Default`
115116

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

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

120121
Hello, {!! $name !!}.
121122

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

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

129-
#### If Statements
130+
#### 条件语句
130131

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

133134
@if (count($records) === 1)
134135
I have one record!
@@ -138,15 +139,15 @@ You may construct `if` statements using the `@if`, `@elseif`, `@else`, and `@end
138139
I don't have any records!
139140
@endif
140141

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

143144
@unless (Auth::check())
144145
You are not signed in.
145146
@endunless
146147

147-
#### Loops
148+
#### 循环
148149

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

151152
@for ($i = 0; $i < 10; $i++)
152153
The current value is {{ $i }}
@@ -166,9 +167,9 @@ In addition to conditional statements, Blade provides simple directives for work
166167
<p>I'm looping forever.</p>
167168
@endwhile
168169

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

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

173174
<div>
174175
@include('shared.errors')
@@ -178,20 +179,20 @@ Blade's `@include` directive, allows you to easily include a Blade view from wit
178179
</form>
179180
</div>
180181

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

183184
@include('view.name', ['some' => 'data'])
184185

185-
#### Comments
186+
#### 注释
186187

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+
Blade 也允许你在视图中注释。但是你的应用程序只能包含HTML注释:
188189

189190
{{-- This comment will not be present in the rendered HTML --}}
190191

191192
<a name="service-injection"></a>
192-
## Service Injection
193+
## 服务注入
193194

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

196197
@inject('metrics', 'App\Services\MetricsService')
197198

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

202203
<a name="extending-blade"></a>
203-
## Extending Blade
204+
## Blade 扩展
204205

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

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

209210
<?php
210211

@@ -238,7 +239,7 @@ The following example creates a `@datetime($var)` directive which formats a give
238239
}
239240
}
240241

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

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

0 commit comments

Comments
 (0)