{"action":"opened","number":46,"pull_request":{"url":"https://api.github.com/repos/laravel-china/docs/pulls/46","id":43955922,"html_url":"https://github.com/laravel-china/docs/pull/46","diff_url":"https://github.com/laravel-china/docs/pull/46.diff","patch_url":"https://github.com/laravel-china/docs/pull/46.patch","issue_url":"https://api.github.com/repos/laravel-china/docs/issues/46","number":46,"state":"open","locked":false,"title":"blade.md","user":{"login":"newworldba","id":7953768,"avatar_url":"https://avatars.githubusercontent.com/u/7953768?v=3","gravatar_id":"","url":"https://api.github.com/users/newworldba","html_url":"https://github.com/newworldba","followers_url":"https://api.github.com/users/newworldba/followers","following_url":"https://api.github.com/users/newworldba/following{/other_user}","gists_url":"https://api.github.com/users/newworldba/gists{/gist_id}","starred_url":"https://api.github.com/users/newworldba/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/newworldba/subscriptions","organizations_url":"https://api.github.com/users/newworldba/orgs","repos_url":"https://api.github.com/users/newworldba/repos","events_url":"https://api.github.com/users/newworldba/events{/privacy}","received_events_url":"https://api.github.com/users/newworldba/received_events","type":"User","site_admin":false},"body":"# 模板Blade\r\n\r\n- [简介](#introduction)\r\n- [模板继承](#template-inheritance)\r\n    - [定义布局](#defining-a-layout)\r\n    - [扩展布局](#extending-a-layout)\r\n- [显示数据](#displaying-data)\r\n- [控制器](#control-structures)\r\n- [服务依赖](#service-injection)\r\n- [Blade扩展](#extending-blade)\r\n\r\n<a name=\"introduction\"></a>\r\n## 简介\r\n\r\nLaravel提供了一个简单强大的模板引擎. 与其他的PHP模板引擎不一样的是，Blade在Views中允许使用PHP代码. Blade视图发生变化时，就会被解析成PHP代码，并且缓存起来，所以Blade不会对你的应用程序产生任何负担。 Blade视图文件以‘blade.php’结尾，所有文件都放在'resources/views'文件夹中。\r\n\r\n<a name=\"template-inheritance\"></a>\r\n## 模板继承\r\n\r\n<a name=\"defining-a-layout\"></a>\r\n### 定义布局\r\n\r\nBlade两个主要的优点就是_模板继承_和_模块化_.在这之前，先看一个简单的例子。首先，我们先看一个“master”布局页面。由于大多数的web应用都使用相同的布局，所以我们只需要定义一种Blade视图。\r\n\r\n    <!-- 文件位置 resources/views/layouts/master.blade.php -->\r\n\r\n    <html>\r\n        <head>\r\n            <title>App Name - @yield('title')</title>\r\n        </head>\r\n        <body>\r\n            @section('sidebar')\r\n                This is the master sidebar.\r\n            @show\r\n\r\n            <div class=\"container\">\r\n                @yield('content')\r\n            </div>\r\n        </body>\r\n    </html>\r\n\r\n这个文件是非常经典的HTTML标记。需要注意的是，这里面有`@section`和`@yield`命令。`@section`和字面上的意思一样，它定义了内容的一部分，而`@yield`命令用来表示给出的section的内容。\r\n\r\n现在我们已经定义了应用的布局，接下来我们对这个布局继承一个子页面。\r\n\r\n<a name=\"extending-a-layout\"></a>\r\n### 布局扩展\r\n\r\n当定义子页面时，我们就可以用Blade的`@extends`命令来说明这个子页面'inherit'（继承）的是哪个。`@extends`一个Blade布局的视图可以将内容注入到用`@section`命令的布局中的某一个部分。记住，在上面的这个例子中，我们是用`@yield`命令来展示布局中的这些部分内容。\r\n\r\n    <!-- 代码路径 resources/views/layouts/child.blade.php -->\r\n\r\n    @extends('layouts.master')\r\n\r\n    @section('title', 'Page Title')\r\n\r\n    @section('sidebar')\r\n        @@parent\r\n\r\n        <p>This is appended to the master sidebar.</p>\r\n    @endsection\r\n\r\n    @section('content')\r\n        <p>This is my body content.</p>\r\n    @endsection\r\n\r\n在这个例子中，'sidebar'部分就是利用`@@parent`命令来将内容追加到布局中的sidebar中。这个视图被渲染时，`@@parent`命令就会被布局中的内容所替换。\r\n\r\n当然，这些简单的PHP视图，路由会调用`view`帮助方法将这些Blade视图返回。\r\n\r\n    Route::get('blade', function () {\r\n        return view('child');\r\n    });\r\n\r\n<a name=\"displaying-data\"></a>\r\n## 展示数据\r\n\r\n在Blade视图中展示数据时，需要将变量放在大括号中括起来。请看下面的路由的例子：\r\n\r\n    Route::get('greeting', function () {\r\n        return view('welcome', ['name' => 'Samantha']);\r\n    });\r\n\r\n如果想展示`name`的内容，可以用下面的方法：\r\n\r\n    Hello, {{ $name }}.\r\n\r\n当然，你不会被局限于使用视图中的变量，你也可以用PHP方法输出结果。事实上，你在Blade中可以任意使用PHP代码来输出语句：\r\n\r\n    The current UNIX timestamp is {{ time() }}.\r\n\r\n> **Note:** Blade 中的`{{}}`语句会自动调用PHP的`htmlentities`方法来抵御XSS攻击。\r\n\r\n#### Blade & JavaScript 框架\r\n\r\n由于JavaScript框架在浏览器中大都使用大括号来表示表达式，你可以使用`@`符号来告诉Blade引擎这个表达式不需要解析。例如：\r\n\r\n    <h1>Laravel</h1>\r\n\r\n    Hello, @{{ name }}.\r\n\r\n这个例子中，这个`@`符号在Blade中会被删掉；然而，`{{name}}`表达式在Blade引擎中会保持不变，这样JavaScript框架就会对它进行处理。\r\n\r\n#### 三元运算\r\n\r\n有的时候你想输入一个变量时，但是你可能不确定这个变量是否被定义了。在PHP代码中，我们就需要这么写，但是这样有点啰嗦：\r\n\r\n    {{ isset($name) ? $name : 'Default' }}\r\n\r\n高兴的是，Blade提供了一个简单快捷的三元运算表示方法。\r\n\r\n    {{ $name or 'Default' }}\r\n\r\n这个例子中，`$name`如果存在，就会被展示。如果不存在，就会输出一个默认的`Default`。\r\n\r\n#### 展示非转义的数据\r\n\r\n默认情况下，Blade 中的`{{ }}`语句会自动调用PHP中的`htmlentities`方法转义来抵御XSS攻击。如果你不想让数据被转义，可以用下面的语法：\r\n\r\n    Hello, {!! $name !!}.\r\n\r\n> **Note:** 在应用程序中输出用户的输入的数据要非常小心。我们经常会用双花括号表示我们需要转义成HTML实体。\r\n\r\n<a name=\"control-structures\"></a>\r\n## 控制结构\r\n\r\n除了模板集成和展示动态数据，Blade也提供了简单的PHP结构控制结构，比如条件语句和循环语句。这些短标签和PHP控制语句一起使用，非常简洁优雅，对PHP非常友好。\r\n\r\n#### 条件语句\r\n\r\n条件语句中可以使用`@if`,`@elseif`,`@endif`命令，这些命令跟PHP的使用方式一样：\r\n\r\n    @if (count($records) === 1)\r\n        I have one record!\r\n    @elseif (count($records) > 1)\r\n        I have multiple records!\r\n    @else\r\n        I don't have any records!\r\n    @endif\r\n\r\n为了更方便，Blade还提供了一个`@unless`命令：\r\n\r\n    @unless (Auth::check())\r\n        You are not signed in.\r\n    @endunless\r\n\r\n#### 循环\r\n\r\n除了条件语句，Blade还提供了一个简单的命令来对应PHP中的循环结构。他们跟PHP的用法是一样的：\r\n\r\n    @for ($i = 0; $i < 10; $i++)\r\n        The current value is {{ $i }}\r\n    @endfor\r\n\r\n    @foreach ($users as $user)\r\n        <p>This is user {{ $user->id }}</p>\r\n    @endforeach\r\n\r\n    @forelse ($users as $user)\r\n        <li>{{ $user->name }}</li>\r\n    @empty\r\n        <p>No users</p>\r\n    @endforelse\r\n\r\n    @while (true)\r\n        <p>I'm looping forever.</p>\r\n    @endwhile\r\n\r\n#### 包含子视图\r\n\r\nBlade中的`@include`命令，让你在一个已有的视图里面包含一个Blade视图变得更加容易。被包含的视图可以使用所有的父级视图中的变量：\r\n\r\n    <div>\r\n        @include('shared.errors')\r\n\r\n        <form>\r\n            <!-- Form Contents -->\r\n        </form>\r\n    </div>\r\n\r\n尽管包含的视图可以继承父级视图的所有数据变量，你也仍然可以在包含的视图中对数据进行扩展：\r\n\r\n    @include('view.name', ['some' => 'data'])\r\n\r\n#### 注释\r\n\r\nBlade 也允许你在视图中注释。但是你的应用程序只能包含HTML注释：\r\n\r\n    {{-- This comment will not be present in the rendered HTML --}}\r\n\r\n<a name=\"service-injection\"></a>\r\n## 服务注入\r\n\r\n`@inject` 可以将Laravel中的服务[服务容器](/docs/{{version}}/container)注入进来.`@inject`的第一个参数将会被定义成service的名称，第二个参数是用来被处理的class/interface:\r\n\r\n    @inject('metrics', 'App\\Services\\MetricsService')\r\n\r\n    <div>\r\n        Monthly Revenue: {{ $metrics->monthlyRevenue() }}.\r\n    </div>\r\n\r\n<a name=\"extending-blade\"></a>\r\n## Blade 扩展\r\n\r\nBlade甚至允许你定义你的习惯用法。你可以使用`directive`方式来注册一个命令。当Blade编译器遇到这个命令时，他就会调用回调方法来处理这些参数。\r\n\r\n下面的例子就是创建了一个`@datetime($var)`的命令来格式化给定的`$var`：\r\n\r\n    <?php\r\n\r\n    namespace App\\Providers;\r\n\r\n    use Blade;\r\n    use Illuminate\\Support\\ServiceProvider;\r\n\r\n    class AppServiceProvider extends ServiceProvider\r\n    {\r\n        /**\r\n         * Perform post-registration booting of services.\r\n         *\r\n         * @return void\r\n         */\r\n        public function boot()\r\n        {\r\n            Blade::directive('datetime', function($expression) {\r\n                return \"<?php echo with{$expression}->format('m/d/Y H:i'); ?>\";\r\n            });\r\n        }\r\n\r\n        /**\r\n         * Register bindings in the container.\r\n         *\r\n         * @return void\r\n         */\r\n        public function register()\r\n        {\r\n            //\r\n        }\r\n    }\r\n\r\n你可以看到，Laravel中的`with`帮助方法在下面的语句中会被使用。这个`with`方法用链式方式简单的返回一个object/value，最终的PHP语句是这样的：\r\n\r\n    <?php echo with($var)->format('m/d/Y H:i'); ?>\r\n\r\n","created_at":"2015-09-02T05:47:13Z","updated_at":"2015-09-02T05:47:13Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/laravel-china/docs/pulls/46/commits","review_comments_url":"https://api.github.com/repos/laravel-china/docs/pulls/46/comments","review_comment_url":"https://api.github.com/repos/laravel-china/docs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/laravel-china/docs/issues/46/comments","statuses_url":"https://api.github.com/repos/laravel-china/docs/statuses/f0c1209813cfd1bb97ff5875730406dff6231547","head":{"label":"newworldba:5.1","ref":"5.1","sha":"f0c1209813cfd1bb97ff5875730406dff6231547","user":{"login":"newworldba","id":7953768,"avatar_url":"https://avatars.githubusercontent.com/u/7953768?v=3","gravatar_id":"","url":"https://api.github.com/users/newworldba","html_url":"https://github.com/newworldba","followers_url":"https://api.github.com/users/newworldba/followers","following_url":"https://api.github.com/users/newworldba/following{/other_user}","gists_url":"https://api.github.com/users/newworldba/gists{/gist_id}","starred_url":"https://api.github.com/users/newworldba/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/newworldba/subscriptions","organizations_url":"https://api.github.com/users/newworldba/orgs","repos_url":"https://api.github.com/users/newworldba/repos","events_url":"https://api.github.com/users/newworldba/events{/privacy}","received_events_url":"https://api.github.com/users/newworldba/received_events","type":"User","site_admin":false},"repo":{"id":41779934,"name":"docs","full_name":"newworldba/docs","owner":{"login":"newworldba","id":7953768,"avatar_url":"https://avatars.githubusercontent.com/u/7953768?v=3","gravatar_id":"","url":"https://api.github.com/users/newworldba","html_url":"https://github.com/newworldba","followers_url":"https://api.github.com/users/newworldba/followers","following_url":"https://api.github.com/users/newworldba/following{/other_user}","gists_url":"https://api.github.com/users/newworldba/gists{/gist_id}","starred_url":"https://api.github.com/users/newworldba/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/newworldba/subscriptions","organizations_url":"https://api.github.com/users/newworldba/orgs","repos_url":"https://api.github.com/users/newworldba/repos","events_url":"https://api.github.com/users/newworldba/events{/privacy}","received_events_url":"https://api.github.com/users/newworldba/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/newworldba/docs","description":"Laravel 的中文翻译. Chinese Translation of Laravel docs http://laravel-china.org/docs","fork":true,"url":"https://api.github.com/repos/newworldba/docs","forks_url":"https://api.github.com/repos/newworldba/docs/forks","keys_url":"https://api.github.com/repos/newworldba/docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/newworldba/docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/newworldba/docs/teams","hooks_url":"https://api.github.com/repos/newworldba/docs/hooks","issue_events_url":"https://api.github.com/repos/newworldba/docs/issues/events{/number}","events_url":"https://api.github.com/repos/newworldba/docs/events","assignees_url":"https://api.github.com/repos/newworldba/docs/assignees{/user}","branches_url":"https://api.github.com/repos/newworldba/docs/branches{/branch}","tags_url":"https://api.github.com/repos/newworldba/docs/tags","blobs_url":"https://api.github.com/repos/newworldba/docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/newworldba/docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/newworldba/docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/newworldba/docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/newworldba/docs/statuses/{sha}","languages_url":"https://api.github.com/repos/newworldba/docs/languages","stargazers_url":"https://api.github.com/repos/newworldba/docs/stargazers","contributors_url":"https://api.github.com/repos/newworldba/docs/contributors","subscribers_url":"https://api.github.com/repos/newworldba/docs/subscribers","subscription_url":"https://api.github.com/repos/newworldba/docs/subscription","commits_url":"https://api.github.com/repos/newworldba/docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/newworldba/docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/newworldba/docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/newworldba/docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/newworldba/docs/contents/{+path}","compare_url":"https://api.github.com/repos/newworldba/docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/newworldba/docs/merges","archive_url":"https://api.github.com/repos/newworldba/docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/newworldba/docs/downloads","issues_url":"https://api.github.com/repos/newworldba/docs/issues{/number}","pulls_url":"https://api.github.com/repos/newworldba/docs/pulls{/number}","milestones_url":"https://api.github.com/repos/newworldba/docs/milestones{/number}","notifications_url":"https://api.github.com/repos/newworldba/docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/newworldba/docs/labels{/name}","releases_url":"https://api.github.com/repos/newworldba/docs/releases{/id}","created_at":"2015-09-02T04:31:00Z","updated_at":"2015-09-01T10:30:15Z","pushed_at":"2015-09-02T05:46:05Z","git_url":"git://github.com/newworldba/docs.git","ssh_url":"git@github.com:newworldba/docs.git","clone_url":"https://github.com/newworldba/docs.git","svn_url":"https://github.com/newworldba/docs","homepage":"","size":5538,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"5.1"}},"base":{"label":"laravel-china:5.1","ref":"5.1","sha":"6b813eda2ca0e9cd09518edd1d62d03222ce2cd4","user":{"login":"laravel-china","id":8603319,"avatar_url":"https://avatars.githubusercontent.com/u/8603319?v=3","gravatar_id":"","url":"https://api.github.com/users/laravel-china","html_url":"https://github.com/laravel-china","followers_url":"https://api.github.com/users/laravel-china/followers","following_url":"https://api.github.com/users/laravel-china/following{/other_user}","gists_url":"https://api.github.com/users/laravel-china/gists{/gist_id}","starred_url":"https://api.github.com/users/laravel-china/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/laravel-china/subscriptions","organizations_url":"https://api.github.com/users/laravel-china/orgs","repos_url":"https://api.github.com/users/laravel-china/repos","events_url":"https://api.github.com/users/laravel-china/events{/privacy}","received_events_url":"https://api.github.com/users/laravel-china/received_events","type":"Organization","site_admin":false},"repo":{"id":23525431,"name":"docs","full_name":"laravel-china/docs","owner":{"login":"laravel-china","id":8603319,"avatar_url":"https://avatars.githubusercontent.com/u/8603319?v=3","gravatar_id":"","url":"https://api.github.com/users/laravel-china","html_url":"https://github.com/laravel-china","followers_url":"https://api.github.com/users/laravel-china/followers","following_url":"https://api.github.com/users/laravel-china/following{/other_user}","gists_url":"https://api.github.com/users/laravel-china/gists{/gist_id}","starred_url":"https://api.github.com/users/laravel-china/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/laravel-china/subscriptions","organizations_url":"https://api.github.com/users/laravel-china/orgs","repos_url":"https://api.github.com/users/laravel-china/repos","events_url":"https://api.github.com/users/laravel-china/events{/privacy}","received_events_url":"https://api.github.com/users/laravel-china/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/laravel-china/docs","description":"Laravel 的中文翻译. Chinese Translation of Laravel docs http://laravel-china.org/docs","fork":true,"url":"https://api.github.com/repos/laravel-china/docs","forks_url":"https://api.github.com/repos/laravel-china/docs/forks","keys_url":"https://api.github.com/repos/laravel-china/docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/laravel-china/docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/laravel-china/docs/teams","hooks_url":"https://api.github.com/repos/laravel-china/docs/hooks","issue_events_url":"https://api.github.com/repos/laravel-china/docs/issues/events{/number}","events_url":"https://api.github.com/repos/laravel-china/docs/events","assignees_url":"https://api.github.com/repos/laravel-china/docs/assignees{/user}","branches_url":"https://api.github.com/repos/laravel-china/docs/branches{/branch}","tags_url":"https://api.github.com/repos/laravel-china/docs/tags","blobs_url":"https://api.github.com/repos/laravel-china/docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/laravel-china/docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/laravel-china/docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/laravel-china/docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/laravel-china/docs/statuses/{sha}","languages_url":"https://api.github.com/repos/laravel-china/docs/languages","stargazers_url":"https://api.github.com/repos/laravel-china/docs/stargazers","contributors_url":"https://api.github.com/repos/laravel-china/docs/contributors","subscribers_url":"https://api.github.com/repos/laravel-china/docs/subscribers","subscription_url":"https://api.github.com/repos/laravel-china/docs/subscription","commits_url":"https://api.github.com/repos/laravel-china/docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/laravel-china/docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/laravel-china/docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/laravel-china/docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/laravel-china/docs/contents/{+path}","compare_url":"https://api.github.com/repos/laravel-china/docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/laravel-china/docs/merges","archive_url":"https://api.github.com/repos/laravel-china/docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/laravel-china/docs/downloads","issues_url":"https://api.github.com/repos/laravel-china/docs/issues{/number}","pulls_url":"https://api.github.com/repos/laravel-china/docs/pulls{/number}","milestones_url":"https://api.github.com/repos/laravel-china/docs/milestones{/number}","notifications_url":"https://api.github.com/repos/laravel-china/docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/laravel-china/docs/labels{/name}","releases_url":"https://api.github.com/repos/laravel-china/docs/releases{/id}","created_at":"2014-08-31T23:36:25Z","updated_at":"2015-09-01T10:30:15Z","pushed_at":"2015-09-02T05:47:13Z","git_url":"git://github.com/laravel-china/docs.git","ssh_url":"git@github.com:laravel-china/docs.git","clone_url":"https://github.com/laravel-china/docs.git","svn_url":"https://github.com/laravel-china/docs","homepage":"","size":5538,"stargazers_count":160,"watchers_count":160,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":106,"mirror_url":null,"open_issues_count":2,"forks":106,"open_issues":2,"watchers":160,"default_branch":"5.1"}},"_links":{"self":{"href":"https://api.github.com/repos/laravel-china/docs/pulls/46"},"html":{"href":"https://github.com/laravel-china/docs/pull/46"},"issue":{"href":"https://api.github.com/repos/laravel-china/docs/issues/46"},"comments":{"href":"https://api.github.com/repos/laravel-china/docs/issues/46/comments"},"review_comments":{"href":"https://api.github.com/repos/laravel-china/docs/pulls/46/comments"},"review_comment":{"href":"https://api.github.com/repos/laravel-china/docs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/laravel-china/docs/pulls/46/commits"},"statuses":{"href":"https://api.github.com/repos/laravel-china/docs/statuses/f0c1209813cfd1bb97ff5875730406dff6231547"}},"merged":false,"mergeable":null,"mergeable_state":"unknown","merged_by":null,"comments":0,"review_comments":0,"commits":2,"additions":55,"deletions":55,"changed_files":1}}