Laravel 5刀片模板中的PHP代码 [英] PHP code inside a Laravel 5 blade template

查看:189
本文介绍了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.

如果以上两种解决方案都不适合,您就会被贡萨洛(Gonzalo).

If neither of the above solutions is suitable, you are stuck with the answers given by Armen and by Gonzalo.

这篇关于Laravel 5刀片模板中的PHP代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆