21

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(function($value)
{
  return preg_replace('/(\s*)@(break|continue)(\s*)/', '$1<?php $2; ?>$3', $value);
});

I have no idea however where to place it, any help would be appreciated?

Vincent
  • 331
  • 1
  • 3
  • 9

3 Answers3

34

Laravel 5 alternative

1) create app/Providers/BladeServiceProvider.php

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class BladeServiceProvider extends ServiceProvider
{
    public function boot()
    {
        /* @datetime($var) */
        \Blade::extend(function($view, $compiler)
        {
            $pattern = $compiler->createOpenMatcher('datetime');

            return preg_replace($pattern, '$1<?php echo $2->format(\'m/d/Y H:i\')); ?>', $view);
        });

        /* @eval($var++) */
        \Blade::extend(function($view)
        {
            return preg_replace('/\@eval\((.+)\)/', '<?php ${1}; ?>', $view);
        });
    }

    public function register()
    {
        //
    }
}

2) in config/app.php add

<?php

return [

    // ...

    'providers' => [

        // ...

        '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, '$1<?php echo $2->format(\'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.

slp
  • 376
  • 3
  • 5
19

There's no requirement telling you where you should put the code, you could even put it in your routes.php (which is a bit messy of course). You only have to make sure that it's loaded when laravel processes a page view.

In this case, creating a new file blade_extensions.php somewhere and including it in start/global.php might be a good solution.

PS: Be sure to clear out your compiled views, as Blade only recompiles the views if it detects a change, so if you've just plonked in this code it won't work until you clear out the views.

ciruvan
  • 5,143
  • 1
  • 26
  • 32
8

Laravel 5 update:

1) You may want to create Extensions\BladeExtensions.php folder\file on the same level as Models, Providers, Services folders

2) BladeExtensions.php

<?php namespace App\Extensions;

class BladeExtensions {

    public static function register()
    {
        \Blade::extend(function($view, $compiler)
        {
            $pattern = $compiler->createMatcher('datetime');

            return preg_replace($pattern, '$1<?php echo $2->format(\'m/d/Y H:i\'); ?>', $view);
        });
    }

}

3) AppServiceProvider.php

// ...
use App\Extensions\BladeExtensions;

class AppServiceProvider extends ServiceProvider
{
    // ...
    public function register()
    {
        // ...

        BladeExtensions::register();
    }

}
sbedulin
  • 4,102
  • 24
  • 34
  • Wow such a luck, I was searching for this about 10 mins and you answered to this old question :) thank you – Sn0opr Feb 09 '15 at 16:51
  • It doesn't work for me, I did a "composer dump-autoload" but nothing happens. – Sn0opr Feb 09 '15 at 17:02
  • `blade` templates are cached, try to make a dummy change in blade, this way laravel will recompile the template – sbedulin Feb 09 '15 at 17:43
  • 2
    To remove cached templates you may also clear `storage\framework\views` and `storage\framework\cache` folders – sbedulin Oct 26 '15 at 07:45