【发布时间】:2018-06-25 15:03:44
【问题描述】:
我正在开发基于 Laravel 5.4 框架的 PHP 项目。我想知道是否有办法将 switch 语句放入包含调用中。
我正在尝试做这样的事情:
@include('components/notification_message/_notification_message_html'
, [ 'notificationType' => $notificationType
, 'notificationMessage' => $notificationMessage
, 'iconType' => switch($notificationType){case 'success': 'check'; break; case 'warning': 'warning'; break; default: 'exclamation';break;}])
如您所料,我在尝试运行它后遇到了语法错误。 知道我应该怎么做吗?
【问题讨论】:
-
也许你想在每个里面使用
@if@elseif@elseif@else@endif和@include。最简单的解决方案。每个人只要浏览一下代码就会明白。 -
为什么不把开关放在
components/notification_message/_notification_message_html? -
这两个想法都很好,我已经尝试过并且奏效了。但我试图找到一些花哨的东西。如果没有其他人有更好的主意,我会将您的答案标记为好。
-
一堆
?:可能是例如$notificationType === 'success' ? 'check' : ($notificationType === 'warning' ? 'warning' : ...) -
是的。这就是我一直在寻找的。谢谢!
标签: php laravel switch-statement laravel-blade