在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语句.您可以使用Laravel或使用普通的PHP开关.您可以像其他任何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