在 Laravel 的 Blade 模版中定义变量
Jun 23, 2016 pm 01:12 PM
有时候我们需要在 Laravel 的 Blade 模版中定义一些变量,而 Blade 却没有提供这样的方法,所以我们这里为大家分享两种可以实现在 Blade 模版中定义变量的方法。
方法一
由于 Blade 模版中允许使用原生 PHP 代码,所以我们可以使用 PHP 语句来定义变量:
<?php $var = 'test'; ?>{{ $var }}
方法二
除了上面的方法,我们还可以使用 Blade 的注释语法来定义/设置变量。由于在 Blade 中 {{-- 这里是注释 --}}会被解析为 ,所以我们可以使用下面这样的语句来定义变量:
{{-- */$i=0;/* --}}// 这条语句会被 Blade 解析为<?php /* */$i=0;/* */ ?>
当然,我们还可以通过扩展 Blade 模版引擎的方法来实现,具体扩展方法可以参考 官方文档。

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon
