Where to place Blade::extend

前端 未结 3 1308
眼角桃花
眼角桃花 2020-12-05 18:29

I want to add the following code to my laravel project to support the break and continue statements in blade.

This is the code:

Blade::extend(functio         


        
3条回答
  •  渐次进展
    2020-12-05 19:18

    Laravel 5 alternative

    1) create app/Providers/BladeServiceProvider.php

    createOpenMatcher('datetime');
    
                return preg_replace($pattern, '$1format(\'m/d/Y H:i\')); ?>', $view);
            });
    
            /* @eval($var++) */
            \Blade::extend(function($view)
            {
                return preg_replace('/\@eval\((.+)\)/', '', $view);
            });
        }
    
        public function register()
        {
            //
        }
    }
    

    2) in config/app.php add

     [
    
            // ...
    
            'App\Providers\BladeServiceProvider',
    

    3) run php artisan clear-compiled

    4) in your template use @datetime($updated_at) or @eval($var = 1), @eval($var++) for example

    5) important remark

    blade templates are cached, try to make a dummy change in blade, this way laravel will recompile the template – sbedulin Feb 9 at 17:43

    In addition to sbedulin's great solution for Laravel 5

    a) run php artisan clear-compiled might be helpful

    b) I changed the code for

    $pattern = $compiler->createOpenMatcher('datetime');

    and

    return preg_replace($pattern, '$1format(\'m/d/Y H:i\')); ?>', $view);

    because the example from the Laravel 5 Documentation will not work.

    The example is corrected now.

    The example was removed.

提交回复
热议问题