php 在 Laravel 5 中切换 - Blade
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29897508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch in Laravel 5 - Blade
提问by ventaquil
How can I use switch in blade templates? When I used:
如何在刀片模板中使用 switch?当我使用:
@switch($login_error)
@case(1)
`E-mail` input is empty!
@break
@case(2)
`Password` input is empty!
@break
@endswitch
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.
结果我看到这个文本为纯文本。我更喜欢在几段代码中使用 switch,因为它比我使用 if 时更干净。
But if it's not possible just write it.
但如果不可能,就写吧。
回答by Margus Pala
Updated 2020 Answer
更新的 2020 答案
Since Laravel 5.5 the @switch is built into the Blade. Use it as shown below.
从 Laravel 5.5 开始,@switch 内置于 Blade 中。如下图所示使用。
@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
Older Answer
较旧的答案
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.
不幸的是 Laravel Blade 没有 switch 语句。如果其他方法或使用普通 PHP 开关,您可以使用 Laravel。您可以像在任何其他 PHP 应用程序中一样在刀片模板中使用纯 PHP。从 Laravel 5.2 开始使用 @php 语句。
Option 1:
选项1:
@if ($login_error == 1)
`E-mail` input is empty!
@elseif ($login_error == 2)
`Password` input is empty!
@endif
回答by Germey
You can just add these code in AppServiceProvider class boot method.
您可以在 AppServiceProvider 类启动方法中添加这些代码。
Blade::extend(function($value, $compiler){
$value = preg_replace('/(\s*)@switch\((.*)\)(?=\s)/', '<?php switch():', $value);
$value = preg_replace('/(\s*)@endswitch(?=\s)/', 'endswitch; ?>', $value);
$value = preg_replace('/(\s*)@case\((.*)\)(?=\s)/', 'case : ?>', $value);
$value = preg_replace('/(?<=\s)@default(?=\s)/', 'default: ?>', $value);
$value = preg_replace('/(?<=\s)@breakswitch(?=\s)/', '<?php break;', $value);
return $value;
});
then you can use as:
那么你可以用作:
@switch( $item )
@case( condition_1 )
// do something
@breakswitch
@case( condition_2 )
// do something else
@breakswitch
@default
// do default behaviour
@breakswitch
@endswitch
Enjoy It~
享受吧~
回答by captainblack
IN LARAVEL 5.2 AND UP:
在 Laravel 5.2 及更高版本中:
Write your usual code between the opening and closing PHP statements.
在开始和结束 PHP 语句之间编写您常用的代码。
@php
switch (x) {
case 1:
//code to be executed
break;
default:
//code to be executed
}
@endphp
回答by JeffK
In Laravel 5.1, this works in a Blade:
在 Laravel 5.1 中,这适用于 Blade:
<?php
switch( $machine->disposal ) {
case 'DISPO': echo 'Send to Property Disposition'; break;
case 'UNIT': echo 'Send to Unit'; break;
case 'CASCADE': echo 'Cascade the machine'; break;
case 'TBD': echo 'To Be Determined (TBD)'; break;
}
?>
回答by Bader
This is now built in Laravel 5.5 https://laravel.com/docs/5.5/blade#switch-statements
这现在内置于 Laravel 5.5 https://laravel.com/docs/5.5/blade#switch-statements
回答by apokryfos
You can extend blade like so:
您可以像这样扩展刀片:
Blade::directive('switch', function ($expression) {
return "<?php switch($expression): ?>";
});
Blade::directive('case', function ($expression) {
return "<?php case $expression: ?>";
});
Blade::directive('break', function () {
return "<?php break; ?>";
});
Blade::directive('default', function () {
return "<?php default: ?>";
});
Blade::directive('endswitch', function () {
return "<?php endswitch; ?>";
});
You can then use the following:
然后,您可以使用以下内容:
@switch($test)
@case(1)
Words
@break
@case(2)
Other Words
@break
@default
Default words
@endswitch
However do note the warnings in : http://php.net/manual/en/control-structures.alternative-syntax.php
但是请注意以下警告:http: //php.net/manual/en/control-structures.alternative-syntax.php
If there is any whitespace between the switch(): and the first case then the whole code block will fail. That is a PHP limitation rather than a blade limitation. You may be able to bypass it by forcing the normal syntax e.g.:
如果 switch(): 和第一种情况之间有任何空格,则整个代码块都将失败。这是 PHP 限制而不是刀片限制。您可以通过强制使用正常语法来绕过它,例如:
Blade::directive('switch', function ($expression) {
return "<?php switch($expression) { ?>";
});
Blade::directive('endswitch', function ($) {
return "<?php } ?>";
});
But this feels a bit wrong.
但这感觉有点不对。
回答by Thomas W. Effendi
To overcome the space in 'switch ()', you can use code :
为了克服'switch()'中的空间,您可以使用代码:
Blade::extend(function($value, $compiler){
$value = preg_replace('/(\s*)@switch[ ]*\((.*)\)(?=\s)/', '<?php switch():', $value);
$value = preg_replace('/(\s*)@endswitch(?=\s)/', 'endswitch; ?>', $value);
$value = preg_replace('/(\s*)@case[ ]*\((.*)\)(?=\s)/', 'case : ?>', $value);
$value = preg_replace('/(?<=\s)@default(?=\s)/', 'default: ?>', $value);
$value = preg_replace('/(?<=\s)@breakswitch(?=\s)/', '<?php break;', $value);
return $value;
});
回答by Kreshnik Hasanaj
When you start using switch statements within your views, that usually indicate that you can further re-factor your code. Business logic is not meant for views, I would rather suggest you to do the switch statement within your controller and then pass the switch statements outcome to the view.
当您开始在视图中使用 switch 语句时,这通常表明您可以进一步重构代码。业务逻辑不适用于视图,我建议您在控制器中执行 switch 语句,然后将 switch 语句的结果传递给视图。