Laravel 5-블레이드 전환
블레이드 템플릿에서 스위치를 어떻게 사용할 수 있습니까? 내가 사용했을 때 :
@switch($login_error)
@case(1)
`E-mail` input is empty!
@break
@case(2)
`Password` input is empty!
@break
@endswitch
결과적으로이 텍스트를 일반 텍스트로 봅니다. 나는 if를 사용할 때보 다 나에게 더 깨끗하기 때문에 몇 가지 코드에서 스위치를 사용하는 것을 선호합니다.
하지만 가능하지 않다면 그냥 쓰세요.
업데이트 된 2019 답변
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
이전 답변
불행히도 Laravel Blade에는 switch 문이 없습니다. 그렇지 않으면 Laravel을 사용하거나 일반 PHP 스위치를 사용할 수 있습니다. 다른 PHP 애플리케이션과 마찬가지로 블레이드 템플릿에서 일반 PHP를 사용할 수 있습니다. Laravel 5.2 이상부터 @php 문을 사용하십시오.
옵션 1:
@if ($login_error == 1)
`E-mail` input is empty!
@elseif ($login_error == 2)
`Password` input is empty!
@endif
AppServiceProvider 클래스 부팅 메서드에 이러한 코드를 추가하기 만하면됩니다.
Blade::extend(function($value, $compiler){
$value = preg_replace('/(\s*)@switch\((.*)\)(?=\s)/', '$1<?php switch($2):', $value);
$value = preg_replace('/(\s*)@endswitch(?=\s)/', '$1endswitch; ?>', $value);
$value = preg_replace('/(\s*)@case\((.*)\)(?=\s)/', '$1case $2: ?>', $value);
$value = preg_replace('/(?<=\s)@default(?=\s)/', 'default: ?>', $value);
$value = preg_replace('/(?<=\s)@breakswitch(?=\s)/', '<?php break;', $value);
return $value;
});
그런 다음 다음과 같이 사용할 수 있습니다.
@switch( $item )
@case( condition_1 )
// do something
@breakswitch
@case( condition_2 )
// do something else
@breakswitch
@default
// do default behaviour
@breakswitch
@endswitch
즐기세요 ~
LARAVEL 5.2 이상 :
PHP 문을 열고 닫는 사이에 일반적인 코드를 작성합니다.
@php
switch (x) {
case 1:
//code to be executed
break;
default:
//code to be executed
}
@endphp
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;
}
?>
This is now built in Laravel 5.5 https://laravel.com/docs/5.5/blade#switch-statements
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
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.:
Blade::directive('switch', function ($expression) {
return "<?php switch($expression) { ?>";
});
Blade::directive('endswitch', function ($) {
return "<?php } ?>";
});
But this feels a bit wrong.
To overcome the space in 'switch ()', you can use code :
Blade::extend(function($value, $compiler){
$value = preg_replace('/(\s*)@switch[ ]*\((.*)\)(?=\s)/', '$1<?php switch($2):', $value);
$value = preg_replace('/(\s*)@endswitch(?=\s)/', '$1endswitch; ?>', $value);
$value = preg_replace('/(\s*)@case[ ]*\((.*)\)(?=\s)/', '$1case $2: ?>', $value);
$value = preg_replace('/(?<=\s)@default(?=\s)/', 'default: ?>', $value);
$value = preg_replace('/(?<=\s)@breakswitch(?=\s)/', '<?php break;', $value);
return $value;
});
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.
참고URL : https://stackoverflow.com/questions/29897508/switch-in-laravel-5-blade
'IT TIP' 카테고리의 다른 글
Android / Eclipse : res / drawable 폴더에 이미지를 추가하려면 어떻게해야합니까? (0) | 2020.10.28 |
---|---|
자바 스크립트를 사용하여 리디렉션하지 않고 URL 변경 (0) | 2020.10.28 |
Java Date 객체에 대한 Unix epoch 시간 (0) | 2020.10.28 |
xdebug 및 netbeans로 php-cli 스크립트 디버깅? (0) | 2020.10.28 |
Django의 템플릿 내부에서 권한 확인 (0) | 2020.10.28 |