企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
<article><h1>Laravel 的 Blade 模板引擎</h1><ul><li><a href="#introduction">简介</a></li><li><a href="#template-inheritance">模板继承</a><ul><li><a href="#defining-a-layout">定义页面布局</a></li><li><a href="#extending-a-layout">继承页面布局</a></li></ul></li><li><a href="#components-and-slots">组件 &amp; Slots</a></li><li><a href="#displaying-data">显示数据</a><ul><li><a href="#blade-and-javascript-frameworks">Blade &amp; JavaScript 框架</a></li></ul></li><li><a href="#control-structures">控制结构</a><ul><li><a href="#if-statements">If 语句</a></li><li><a href="#loops">循环</a></li><li><a href="#the-loop-variable">循环变量</a></li><li><a href="#comments">注释</a></li><li><a href="#php">PHP</a></li></ul></li><li><a href="#including-sub-views">引入子视图</a><ul><li><a href="#rendering-views-for-collections">为集合渲染视图</a></li></ul></li><li><a href="#stacks">堆栈</a></li><li><a href="#service-injection">服务注入</a></li><li><a href="#extending-blade">扩充 Blade</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p>Blade 是 Laravel 提供的一个既简单又强大的模板引擎。和其他流行的 PHP 模板引擎不一样,Blade 并不限制你在视图中使用原生 PHP 代码。所有 Blade 视图文件都将被编译成原生的 PHP 代码并缓存起来,除非它被修改,否则不会重新编译,这就意味着 Blade 基本上不会给你的应用增加任何额外负担。Blade 视图文件使用 <code class=" language-php"><span class="token punctuation">.</span>blade<span class="token punctuation">.</span>php</code> 扩展名,一般被存放在 <code class=" language-php">resources<span class="token operator">/</span>views</code> 目录。</p><p><a name="template-inheritance"></a></p><h2><a href="#template-inheritance">模板继承</a></h2><p><a name="defining-a-layout"></a></p><h3>定义页面布局</h3><p>Blade 的两个主要优点是 <em>模板继承</em> 和 <em>区块</em> 。</p><p>为方便开始,让我们先通过一个简单的例子来上手。首先,我们需要确认一个 "master" 的页面布局。因为大多数 web 应用是在不同的页面中使用相同的布局方式,我们可以很方便的定义这个 Blade 布局视图:</p><pre class=" language-php"><code class=" language-php"><span class="token markup"><span class="token comment" spellcheck="true">&lt;!-- 文件保存于 resources/views/layouts/app.blade.php --&gt;</span></span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span></span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span></span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span></span>应用程序名称 <span class="token operator">-</span> @<span class="token keyword">yield</span><span class="token punctuation">(</span><span class="token string">'title'</span><span class="token punctuation">)</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span></span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span></span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span></span> @<span class="token function">section<span class="token punctuation">(</span></span><span class="token string">'sidebar'</span><span class="token punctuation">)</span> 这是 master 的侧边栏。 @show <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>container<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span></span> @<span class="token keyword">yield</span><span class="token punctuation">(</span><span class="token string">'content'</span><span class="token punctuation">)</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span></span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span></span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span></span></code></pre><p>如你所见,该文件包含了典型的 HTML 语法。不过,请注意 <code class=" language-php">@section</code> 和 <code class=" language-php">@<span class="token keyword">yield</span></code> 命令。 <code class=" language-php">@section</code> 命令正如其名字所暗示的一样是用来定义一个视图区块的,而 <code class=" language-php">@<span class="token keyword">yield</span></code> 指令是用来显示指定区块的内容的。</p><p>现在,我们已经定义好了这个应用程序的布局,让我们接着来定义一个继承此布局的子页面。</p><p><a name="extending-a-layout"></a></p><h3>继承页面布局</h3><p>当定义子页面时,你可以使用 Blade 提供的 <code class=" language-php">@<span class="token keyword">extends</span></code> 命令来为子页面指定其所 「继承」 的页面布局。 当子页面继承布局之后,即可使用 <code class=" language-php">@section</code> 命令将内容注入于布局的 <code class=" language-php">@section</code> 区块中。切记,在上面的例子里,布局中使用 <code class=" language-php">@<span class="token keyword">yield</span></code> 的地方将会显示这些区块中的内容:</p><pre class=" language-php"><code class=" language-php"><span class="token markup"><span class="token comment" spellcheck="true">&lt;!-- Stored in resources/views/child.blade.php --&gt;</span></span> @<span class="token keyword">extends</span><span class="token punctuation">(</span><span class="token string">'layouts.app'</span><span class="token punctuation">)</span> @<span class="token function">section<span class="token punctuation">(</span></span><span class="token string">'title'</span><span class="token punctuation">,</span> <span class="token string">'Page Title'</span><span class="token punctuation">)</span> @<span class="token function">section<span class="token punctuation">(</span></span><span class="token string">'sidebar'</span><span class="token punctuation">)</span> @@<span class="token keyword">parent</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">&gt;</span></span></span>This is appended to the master sidebar<span class="token punctuation">.</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span></span> @endsection @<span class="token function">section<span class="token punctuation">(</span></span><span class="token string">'content'</span><span class="token punctuation">)</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">&gt;</span></span></span>This is my body content<span class="token punctuation">.</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span></span> @endsection</code></pre><p>在上面的例子里,<code class=" language-php">sidebar</code> 区块利用了 <code class=" language-php">@@<span class="token keyword">parent</span></code> 命令追加布局中的 sidebar 区块中的内容,如果不使用则会覆盖掉布局中的这部分内容。 <code class=" language-php">@@<span class="token keyword">parent</span></code> 命令会在视图被渲染时替换为布局中的内容。</p><p>当然,可以通过在路由中使用全局辅助函数 <code class=" language-php">view</code> 来返回 Blade 视图:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'blade'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'child'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="components-and-slots"></a></p><h2><a href="#components-and-slots">组件 &amp; Slots</a></h2><p>组件和 slots 能提供类似于区块和布局的好处;不过,一些人可能发现组件和 slots 更容易理解。首先,让我们假设一个会在我们应用中重复使用的「警告」组件:</p><pre class=" language-php"><code class=" language-php"><span class="token markup"><span class="token comment" spellcheck="true">&lt;!-- /resources/views/alert.blade.php --&gt;</span></span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>alert alert-danger<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span></span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$slot</span> <span class="token punctuation">}</span><span class="token punctuation">}</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span></span></code></pre><p><code class=" language-php"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$slot</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code> 变量将包含我们希望注入到组件的内容。现在,我们可以使用 <code class=" language-php">@component</code> 指令来构造这个组件:</p><pre class=" language-php"><code class=" language-php">@<span class="token function">component<span class="token punctuation">(</span></span><span class="token string">'alert'</span><span class="token punctuation">)</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>strong</span><span class="token punctuation">&gt;</span></span></span>哇!<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>strong</span><span class="token punctuation">&gt;</span></span></span> 出现了一些问题! @endcomponent</code></pre><p>有些时候它对于定义组件的多个 slots 是非常有帮助的。让我们修改我们的警告组件,让它支持注入一个「标题」。 已命名的 slots 将显示「相对应」名称的变量的值:</p><pre class=" language-php"><code class=" language-php"><span class="token markup"><span class="token comment" spellcheck="true">&lt;!-- /resources/views/alert.blade.php --&gt;</span></span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>alert alert-danger<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span></span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>alert-title<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span></span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$title</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span></span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$slot</span> <span class="token punctuation">}</span><span class="token punctuation">}</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span></span></code></pre><p>现在,我们可以使用 <code class=" language-php">@slot</code> 指令注入内容到已命名的 slot 中,任何没有被 <code class=" language-php">@slot</code> 指令包裹住的内容将传递给组件中的 <code class=" language-php"><span class="token variable">$slot</span></code> 变量:</p><pre class=" language-php"><code class=" language-php">@<span class="token function">component<span class="token punctuation">(</span></span><span class="token string">'alert'</span><span class="token punctuation">)</span> @<span class="token function">slot<span class="token punctuation">(</span></span><span class="token string">'title'</span><span class="token punctuation">)</span> 拒绝 @endslot 你没有权限访问这个资源! @endcomponent</code></pre><h4>传递额外的数据给组件</h4><p>有时候你可能需要传递额外的数据给组件。为了解决这个问题,你可以传递一个数组作为第二个参数传递给 <code class=" language-php">@component</code> 指令。所有的数据都将以变量的形式传递给组件模版:</p><pre class=" language-php"><code class=" language-php">@<span class="token function">component<span class="token punctuation">(</span></span><span class="token string">'alert'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'foo'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> @endcomponent</code></pre><p><a name="displaying-data"></a></p><h2><a href="#displaying-data">显示数据</a></h2><p>你可以使用 「中括号」 包住变量以显示传递至 Blade 视图的数据。如下面的路由设置:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'greeting'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token function">view<span class="token punctuation">(</span></span><span class="token string">'welcome'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'Samantha'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你可以像这样显示 <code class=" language-php">name</code> 变量的内容:</p><pre class=" language-php"><code class=" language-php">Hello<span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$name</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">.</span></code></pre><p>当然也不是说一定只能显示传递至视图的变量内容。你也可以显示 PHP 函数的结果。事实上,你可以在 Blade 中显示任意的 PHP 代码:</p><pre class=" language-php"><code class=" language-php">The current <span class="token constant">UNIX</span> timestamp is <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">time<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">.</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> Blade <code class=" language-php"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code> 语法会自动调用 PHP <code class=" language-php">htmlspecialchars</code> 函数来避免 XSS 攻击。</p></blockquote><h4>当数据存在时输出</h4><p>有时候你可能想要输出一个变量,但是你并不确定这个变量是否已经被定义,我们可以用像这样的冗长 PHP 代码表达:</p><pre class=" language-php"><code class=" language-php"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">isset<span class="token punctuation">(</span></span><span class="token variable">$name</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token variable">$name</span> <span class="token punctuation">:</span> <span class="token string">'Default'</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>事实上,Blade 提供了更便捷的方式来代替这种三元运算符表达式:</p><pre class=" language-php"><code class=" language-php"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$name</span> <span class="token keyword">or</span> <span class="token string">'Default'</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>在这个例子中,如果 <code class=" language-php"><span class="token variable">$name</span></code> 变量存在,它的值将被显示出来。但是,如果它不存在,则会显示 <code class=" language-php"><span class="token keyword">Default</span></code> 。</p><h4>显示未转义过的数据</h4><p>在默认情况下,Blade 模板中的 <code class=" language-php"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code> 表达式将会自动调用 PHP <code class=" language-php">htmlspecialchars</code> 函数来转义数据以避免 XSS 的攻击。如果你不想你的数据被转义,你可以使用下面的语法:</p><pre class=" language-php"><code class=" language-php">Hello<span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token operator">!</span><span class="token operator">!</span> <span class="token variable">$name</span> <span class="token operator">!</span><span class="token operator">!</span><span class="token punctuation">}</span><span class="token punctuation">.</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 要非常小心处理用户输入的数据时,你应该总是使用 <code class=" language-php"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code> 语法来转义内容中的任何的 HTML 元素,以避免 XSS 攻击。</p></blockquote><p><a name="blade-and-javascript-frameworks"></a></p><h3>Blade &amp; JavaScript 框架</h3><p>由于很多 JavaScript 框架都使用花括号来表明所提供的表达式,所以你可以使用 <code class=" language-php">@</code> 符号来告知 Blade 渲染引擎你需要保留这个表达式原始形态,例如:</p><pre class=" language-php"><code class=" language-php"><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">&gt;</span></span></span>Laravel<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span></span> Hello<span class="token punctuation">,</span> @<span class="token punctuation">{</span><span class="token punctuation">{</span> name <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">.</span></code></pre><p>在这个例子里,<code class=" language-php">@</code> 符号最终会被 Blade 引擎剔除,并且 <code class=" language-php"><span class="token punctuation">{</span><span class="token punctuation">{</span> name <span class="token punctuation">}</span><span class="token punctuation">}</span></code> 表达式会被原样的保留下来,这样就允许你的 JavaScript 框架来使用它了。</p><h4><code class=" language-php">@verbatim</code> 指令</h4><p>如果你需要在页面中大片区块中展示 JavaScript 变量,你可以使用 <code class=" language-php">@verbatim</code> 指令来包裹 HTML 内容,这样你就不需要为每个需要解析的变量增加 <code class=" language-php">@</code> 符号前缀了:</p><pre class=" language-php"><code class=" language-php">@verbatim <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>container<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span></span> Hello<span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> name <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">.</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span></span> @endverbatim</code></pre><p><a name="control-structures"></a></p><h2><a href="#control-structures">控制结构</a></h2><p>除了模板继承与数据显示的功能以外,Blade 也给一般的 PHP 结构控制语句提供了方便的缩写,比如条件表达式和循环语句。这些缩写提供了更为清晰简明的方式来使用 PHP 的控制结构,而且还保持与 PHP 语句的相似性。</p><p><a name="if-statements"></a></p><h3>If 语句</h3><p>你可以通过 <code class=" language-php">@<span class="token keyword">if</span></code>, <code class=" language-php">@<span class="token keyword">elseif</span></code>, <code class=" language-php">@<span class="token keyword">else</span></code> 及 <code class=" language-php">@<span class="token keyword">endif</span></code> 指令构建 <code class=" language-php"><span class="token keyword">if</span></code> 表达式。这些命令的功能等同于在 PHP 中的语法:</p><pre class=" language-php"><code class=" language-php">@<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">count<span class="token punctuation">(</span></span><span class="token variable">$records</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token number">1</span><span class="token punctuation">)</span> 我有一条记录! @<span class="token keyword">elseif</span> <span class="token punctuation">(</span><span class="token function">count<span class="token punctuation">(</span></span><span class="token variable">$records</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">1</span><span class="token punctuation">)</span> 我有多条记录! @<span class="token keyword">else</span> 我没有任何记录! @<span class="token keyword">endif</span></code></pre><p>为了方便,Blade 也提供了一个 <code class=" language-php">@unless</code> 命令:</p><pre class=" language-php"><code class=" language-php">@unless <span class="token punctuation">(</span><span class="token scope">Auth<span class="token punctuation">::</span></span><span class="token function">check<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span> 你尚未登录。 @endunless</code></pre><p><a name="loops"></a></p><h3>循环</h3><p>除了条件表达式外,Blade 也支持 PHP 的循环结构,这些命令的功能等同于在 PHP 中的语法:</p><pre class=" language-php"><code class=" language-php">@<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token variable">$i</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token variable">$i</span> <span class="token operator">&lt;</span> <span class="token number">10</span><span class="token punctuation">;</span> <span class="token variable">$i</span><span class="token operator">++</span><span class="token punctuation">)</span> 目前的值为 <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$i</span> <span class="token punctuation">}</span><span class="token punctuation">}</span> @<span class="token keyword">endfor</span> @<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">&gt;</span></span></span>此用户为 <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">id</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span></span> @<span class="token keyword">endforeach</span> @forelse <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span></span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">name</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span></span> @empty <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">&gt;</span></span></span>没有用户<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span></span> @endforelse @<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">&gt;</span></span></span>我永远都在跑循环。<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span></span> @<span class="token keyword">endwhile</span></code></pre><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 当循环时,你可以使用 <a href="#the-loop-variable">循环变量</a> 来获取循环中有价值的信息,比如循环中的首次或最后的迭代。</p></blockquote><p>当使用循环时,你可能也需要一些结束循环或者跳出当前循环的命令:</p><pre class=" language-php"><code class=" language-php">@<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> @<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">type</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> @<span class="token keyword">continue</span> @<span class="token keyword">endif</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span></span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">name</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span></span> @<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">number</span> <span class="token operator">==</span> <span class="token number">5</span><span class="token punctuation">)</span> @<span class="token keyword">break</span> @<span class="token keyword">endif</span> @<span class="token keyword">endforeach</span></code></pre><p>你也可以使用命令声明包含条件的方式在一条语句中达到中断:</p><pre class=" language-php"><code class=" language-php">@<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> @<span class="token keyword">continue</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">type</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span></span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">name</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span></span> @<span class="token keyword">break</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">number</span> <span class="token operator">==</span> <span class="token number">5</span><span class="token punctuation">)</span> @<span class="token keyword">endforeach</span></code></pre><p><a name="the-loop-variable"></a></p><h3>循环变量</h3><p>当循环时,你可以在循环内访问 <code class=" language-php"><span class="token variable">$loop</span></code> 变量。这个变量可以提供一些有用的信息,比如当前循环的索引,当前循环是不是首次迭代,又或者当前循环是不是最后一次迭代:</p><pre class=" language-php"><code class=" language-php">@<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> @<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$loop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">first</span><span class="token punctuation">)</span> This is the first iteration<span class="token punctuation">.</span> @<span class="token keyword">endif</span> @<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$loop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">last</span><span class="token punctuation">)</span> This is the last iteration<span class="token punctuation">.</span> @<span class="token keyword">endif</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">&gt;</span></span></span>This is user <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">id</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">&gt;</span></span></span> @<span class="token keyword">endforeach</span></code></pre><p>如果你是在一个嵌套的循环中,你可以通过使用 <code class=" language-php"><span class="token variable">$loop</span></code> 变量的 <code class=" language-php"><span class="token keyword">parent</span></code> 属性来获取父循环中的 <code class=" language-php"><span class="token variable">$loop</span></code> 变量:</p><pre class=" language-php"><code class=" language-php">@<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> @<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">posts</span> <span class="token keyword">as</span> <span class="token variable">$post</span><span class="token punctuation">)</span> @<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$loop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token keyword">parent</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">first</span><span class="token punctuation">)</span> This is first iteration of the <span class="token keyword">parent</span> loop<span class="token punctuation">.</span> @<span class="token keyword">endif</span> @<span class="token keyword">endforeach</span> @<span class="token keyword">endforeach</span></code></pre><p><code class=" language-php"><span class="token variable">$loop</span></code> 变量也包含了其它各种有用的属性:</p><table><thead><tr><th>属性</th><th>描述</th></tr></thead><tbody><tr><td><code class=" language-php"><span class="token variable">$loop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">index</span></code></td><td>当前循环所迭代的索引,起始为 0。</td></tr><tr><td><code class=" language-php"><span class="token variable">$loop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">iteration</span></code></td><td>当前迭代数,起始为 1。</td></tr><tr><td><code class=" language-php"><span class="token variable">$loop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">remaining</span></code></td><td>循环中迭代剩余的数量。</td></tr><tr><td><code class=" language-php"><span class="token variable">$loop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">count</span></code></td><td>被迭代项的总数量。</td></tr><tr><td><code class=" language-php"><span class="token variable">$loop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">first</span></code></td><td>当前迭代是否是循环中的首次迭代。</td></tr><tr><td><code class=" language-php"><span class="token variable">$loop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">last</span></code></td><td>当前迭代是否是循环中的最后一次迭代。</td></tr><tr><td><code class=" language-php"><span class="token variable">$loop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">depth</span></code></td><td>当前循环的嵌套深度。</td></tr><tr><td><code class=" language-php"><span class="token variable">$loop</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token keyword">parent</span></code></td><td>当在嵌套的循环内时,可以访问到父循环中的 $loop 变量。</td></tr></tbody></table><p><a name="comments"></a></p><h3>注释</h3><p>Blade 也允许在页面中定义注释,然而,跟 HTML 的注释不同的是,Blade 注释不会被包含在应用程序返回的 HTML 内:</p><pre class=" language-php"><code class=" language-php"><span class="token punctuation">{</span><span class="token punctuation">{</span><span class="token operator">--</span> 此注释将不会出现在渲染后的 <span class="token constant">HTML</span> <span class="token operator">--</span><span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p><a name="php"></a></p><h3>PHP</h3><p>在某些情况下,它对于你在视图文件中嵌入 php 代码是非常有帮助的。你可以在你的模版中使用 Blade 提供的 <code class=" language-php">@php</code> 指令来执行一段纯 PHP 代码:</p><pre class=" language-php"><code class=" language-php">@php <span class="token comment" spellcheck="true"> // </span>@endphp</code></pre><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 虽然 Blade 提供了这个功能,但频繁地使用也同时意味着你在你的模版中嵌入了太多的逻辑了。</p></blockquote><p><a name="including-sub-views"></a></p><h2><a href="#including-sub-views">引入子视图</a></h2><p>你可以使用 Blade 的 <code class=" language-php">@<span class="token keyword">include</span></code> 命令来引入一个已存在的视图,所有在父视图的可用变量在被引入的视图中都是可用的。</p><pre class=" language-php"><code class=" language-php"><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span></span> @<span class="token keyword">include</span><span class="token punctuation">(</span><span class="token string">'shared.errors'</span><span class="token punctuation">)</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span><span class="token punctuation">&gt;</span></span></span> <span class="token markup"><span class="token comment" spellcheck="true">&lt;!-- Form Contents --&gt;</span></span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">&gt;</span></span></span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span></span></code></pre><p>尽管被引入的视图会继承父视图中的所有数据,你也可以通过传递额外的数组数据至被引入的页面:</p><pre class=" language-php"><code class=" language-php">@<span class="token keyword">include</span><span class="token punctuation">(</span><span class="token string">'view.name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'some'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><p>当然,如果你尝试使用 <code class=" language-php">@<span class="token keyword">include</span></code> 去引用一个不存在的视图,Laravel 会抛出错误。如果你想引入一个视图,而你又无法确认这个视图存在与否,你可以使用 <code class=" language-php">@includeIf</code> 指令:</p><pre class=" language-php"><code class=" language-php">@<span class="token function">includeIf<span class="token punctuation">(</span></span><span class="token string">'view.name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'some'</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token string">'data'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 请避免在 Blade 视图中使用 <code class=" language-php"><span class="token constant">__DIR__</span></code> 及 <code class=" language-php"><span class="token constant">__FILE__</span></code> 常量,因为他们会引用视图被缓存的位置。</p></blockquote><p><a name="rendering-views-for-collections"></a></p><h3>为集合渲染视图</h3><p>你可以使用 Blade 的 <code class=" language-php">@each</code> 命令将循环及引入结合成一行代码:</p><pre class=" language-php"><code class=" language-php">@<span class="token function">each<span class="token punctuation">(</span></span><span class="token string">'view.name'</span><span class="token punctuation">,</span> <span class="token variable">$jobs</span><span class="token punctuation">,</span> <span class="token string">'job'</span><span class="token punctuation">)</span></code></pre><p>第一个参数为每个元素要渲染的子视图,第二个参数是你要迭代的数组或集合,而第三个参数为迭代时被分配至子视图中的变量名称。举个例子,如果你需要迭代一个 <code class=" language-php">jobs</code> 数组,通常子视图会使用 <code class=" language-php">job</code> 作为变量来访问 job 信息。子视图使用 <code class=" language-php">key</code> 变量作为当前迭代的键名。</p><p>你也可以传递第四个参数到 <code class=" language-php">@each</code> 命令。当需要迭代的数组为空时,将会使用这个参数提供的视图来渲染。</p><pre class=" language-php"><code class=" language-php">@<span class="token function">each<span class="token punctuation">(</span></span><span class="token string">'view.name'</span><span class="token punctuation">,</span> <span class="token variable">$jobs</span><span class="token punctuation">,</span> <span class="token string">'job'</span><span class="token punctuation">,</span> <span class="token string">'view.empty'</span><span class="token punctuation">)</span></code></pre><p><a name="stacks"></a></p><h2><a href="#stacks">堆栈</a></h2><p>Blade 也允许你在其它视图或布局中为已经命名的堆栈中压入数据,这在子视图中引入必备的 JavaScript 类库时尤其有用:</p><pre class=" language-php"><code class=" language-php">@<span class="token function">push<span class="token punctuation">(</span></span><span class="token string">'scripts'</span><span class="token punctuation">)</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>/example.js<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span></span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span></span> @endpush</code></pre><p>你可以根据需要多次压入堆栈,通过 <code class=" language-php">@stack</code> 命令中键入堆栈的名字来渲染整个堆栈:</p><pre class=" language-php"><code class=" language-php"><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span></span> <span class="token markup"><span class="token comment" spellcheck="true">&lt;!-- Head Contents --&gt;</span></span> @<span class="token function">stack<span class="token punctuation">(</span></span><span class="token string">'scripts'</span><span class="token punctuation">)</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span></span></code></pre><p><a name="service-injection"></a></p><h2><a href="#service-injection">服务注入</a></h2><p>你可以使用 <code class=" language-php">@inject</code> 命令来从 Larvel <a href="/docs/5.4/container">service container</a> 中取出服务。传递给 <code class=" language-php">@inject</code> 的第一个参数为置放该服务的变量名称,而第二个参数为你想要解析的服务的类或是接口的名称:</p><pre class=" language-php"><code class=" language-php">@<span class="token function">inject<span class="token punctuation">(</span></span><span class="token string">'metrics'</span><span class="token punctuation">,</span> <span class="token string">'App\Services\MetricsService'</span><span class="token punctuation">)</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span></span> Monthly Revenue<span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$metrics</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">monthlyRevenue<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">.</span> <span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span></span></code></pre><p><a name="extending-blade"></a></p><h2><a href="#extending-blade">拓展 Blade</a></h2><p>Blade 甚至允许你使用 <code class=" language-php">directive</code> 方法来注册自己的命令。当 Blade 编译器遇到该命令时,它将会带参数调用提供的回调函数。</p><p>以下例子会创建一个把指定的 <code class=" language-php"><span class="token variable">$var</span></code> 格式化的 <code class=" language-php">@<span class="token function">datetime<span class="token punctuation">(</span></span><span class="token variable">$var</span><span class="token punctuation">)</span></code> 命令:</p><pre class=" language-php"><code class=" language-php"><span class="token php"><span class="token delimiter">&lt;?php</span> <span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Blade</span><span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">AppServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/** * 运行服务注册后的启动进程。 * * @return void */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">boot<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token scope">Blade<span class="token punctuation">::</span></span><span class="token function">directive<span class="token punctuation">(</span></span><span class="token string">'datetime'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$expression</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> "<span class="token delimiter">&lt;?php</span> <span class="token keyword">echo</span> <span class="token punctuation">(</span><span class="token variable">$expression</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">format<span class="token punctuation">(</span></span><span class="token string">'m/d/Y H:i'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter">?&gt;</span></span>"<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 在容器注册绑定。 * * @return void */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">register<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true"> // </span> <span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre><p>如你所见,我们可以使用链式调用 <code class=" language-php">format</code> 方法的表述方式传递到指令。所以,在这个例子里,最终该指令生成了的 PHP 代码如下:</p><pre class=" language-php"><code class=" language-php"><span class="token php"><span class="token delimiter">&lt;?php</span> <span class="token keyword">echo</span> <span class="token variable">$var</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">format<span class="token punctuation">(</span></span><span class="token string">'m/d/Y H:i'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token delimiter">?&gt;</span></span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 在更新 Blade 指令的逻辑后,你将需要删除所有已缓存的 Blade 视图,使用 <code class=" language-php">view<span class="token punctuation">:</span>clear</code> Artisan 命令来清除被缓存的视图。</p></blockquote></article>