Laravel 5 刀片模板中的 PHP 代码

2021-12-18 00:00:00 php laravel-blade laravel-5

我必须在 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.

相关文章