Switch in Laravel 5 - Blade

后端 未结 8 540
南旧
南旧 2021-02-02 05:10

How can I use switch in blade templates? When I used:

@switch($login_error)
    @case(1)
        `E-mail` input is empty!
        @break
    @case(2)
        `Pa         


        
8条回答
  •  终归单人心
    2021-02-02 05:31

    You can just add these code in AppServiceProvider class boot method.

    Blade::extend(function($value, $compiler){
            $value = preg_replace('/(\s*)@switch\((.*)\)(?=\s)/', '$1', $value);
            $value = preg_replace('/(\s*)@case\((.*)\)(?=\s)/', '$1case $2: ?>', $value);
            $value = preg_replace('/(?<=\s)@default(?=\s)/', 'default: ?>', $value);
            $value = preg_replace('/(?<=\s)@breakswitch(?=\s)/', '

    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~

提交回复
热议问题