Switch in Laravel 5 - Blade(在 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
这篇关于在 Laravel 5 中切换 - 刀片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Laravel 5 中切换 - 刀片


基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01