PHP code inside a Laravel 5 blade template(Laravel 5 刀片模板中的 PHP 代码)
问题描述
我必须在 Laravel 5 刀片模板中放置一些 PHP 代码.如下图
I have to place some PHP code inside a Laravel 5 blade template. Like below
@foreach ($farmer->tasks as $task)
@if ($task->pivot->due_at) < date(now))
$style = 'alert alert-danger';
@elseif ($task->pivot->due_at) > date(now))
$style = 'alert alert-success';
@else
$style = '';
@endif
@endforeach
将 PHP 代码放入 Laravel 5 刀片模板的实际过程是什么?
What is the actual procedure to place PHP code inside a Laravel 5 blade template?
推荐答案
根据文档,在 Laravel 5.2 及更新版本中,您可以使用以下代码:
According to documentation, in Laravel 5.2 and newer you can use the following code:
@php
{{-- PHP code here --}}
@endphp
或者,您可以扩展 Blade 模板引擎,如此处所述.
Alternatively, you can extend the Blade templating engine as it's described here.
如果以上两种解决方案都不合适,那么您会被Armen 和 作者:贡萨洛.
If neither of the above solutions is suitable, you are stuck with the answers given by Armen and by Gonzalo.
这篇关于Laravel 5 刀片模板中的 PHP 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 5 刀片模板中的 PHP 代码
基础教程推荐
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
