在 Laravel 的 Blade 模版中定义变量

WBOY
Release: 2016-06-23 13:12:00
Original
1748 people have browsed it

有时候我们需要在 Laravel 的 Blade 模版中定义一些变量,而 Blade 却没有提供这样的方法,所以我们这里为大家分享两种可以实现在 Blade 模版中定义变量的方法。

方法一

由于 Blade 模版中允许使用原生 PHP 代码,所以我们可以使用 PHP 语句来定义变量:

<?php $var = 'test'; ?>{{ $var }}
Copy after login

方法二

除了上面的方法,我们还可以使用 Blade 的注释语法来定义/设置变量。由于在 Blade 中 {{-- 这里是注释 --}}会被解析为 ,所以我们可以使用下面这样的语句来定义变量:

{{-- */$i=0;/* --}}// 这条语句会被 Blade 解析为<?php /* */$i=0;/* */ ?>
Copy after login

当然,我们还可以通过扩展 Blade 模版引擎的方法来实现,具体扩展方法可以参考 官方文档。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!