在 Laravel 5 中切换 - 刀片
如何在刀片模板中使用 switch?我用的时候:
How can I use switch in blade templates? When I used:
@switch($login_error)
@case(1)
`E-mail` input is empty!
@break
@case(2)
`Password` input is empty!
@break
@endswitch
结果我将此文本视为纯文本.我更喜欢在几段代码中使用 switch,因为它对我来说比使用 if 时更干净.
in result I see this text as plaintext. I prefer to use switch in few piece of code because it's more clean for me than when I using if.
但如果不可能,就写吧.
But if it's not possible just write it.
推荐答案
2020 年更新答案
从 Laravel 5.5 开始,@switch 内置在 Blade 中.如下所示使用它.
Since Laravel 5.5 the @switch is built into the Blade. Use it as shown below.
@switch($login_error)
@case(1)
<span> `E-mail` input is empty!</span>
@break
@case(2)
<span>`Password` input is empty!</span>
@break
@default
<span>Something went wrong, please try again</span>
@endswitch
旧答案
很遗憾 Laravel Blade 没有 switch 语句.如果其他方法或使用普通 PHP 开关,您可以使用 Laravel.您可以像在任何其他 PHP 应用程序中一样在刀片模板中使用纯 PHP.从 Laravel 5.2 及更高版本开始使用 @php 语句.
Unfortunately Laravel Blade does not have switch statement. You can use Laravel if else approach or use use plain PHP switch. You can use plain PHP in blade templates like in any other PHP application. Starting from Laravel 5.2 and up use @php statement.
选项 1:
@if ($login_error == 1)
`E-mail` input is empty!
@elseif ($login_error == 2)
`Password` input is empty!
@endif
相关文章