I would like to add @continue and @break statements for my Blade engine in order to control my loops.

I've seen into the sources an extend function for the BladeEngine, and I've tried to use it in my routes.php file:

Blade::extend(function ($value) {
    $pattern = Blade::createMatcher('continue');
    return preg_replace($pattern, '$1<?php continue; ?>$2', $value);
});

One of my views:

@if (isset($meta['foo']) && !$meta['bar'])
    @continue
@else
    <li>{{$meta['pseudo']}}</li>
@endif

But the rendered HTML page shows me @continue.

Any idea of how make it work?

shareimprove this question
 
   
Where did you add this Blade::extend command? – Dennis Braga Feb 3 '14 at 16:52
   
@DennisBraga It's the first line of my file route.php – Fractaliste Feb 3 '14 at 19:48

1 Answer 正确答案 

Did you clear cache/compiled view files after adding the provided piece of your code to routes.php ? If not, try doing so as Blade will recompile the views only if changes are detected in them. So if you didn't clear the compiled views after adding the code, nothing changed in the rendered html.

If it's not the case, try using plain old regexp instead of the Blade::createMatcher, this nice definition will provide you with both continue and break support in a oneliner.

Blade::extend(function($value)
{
  return preg_replace('/(\s*)@(break|continue)(\s*)/', '$1<?php $2; ?>$3', $value);
});

It should work even if placed in routes.php, although it's better to put it in a separate file (for example blade.php and include it in global.php). Anyway it must be loaded prior to the view being processed.

shareimprove this answer
 
   
Thank you very much, I've already checked the cache, but I didn't know the compiled view folder. It was the first reason of my problem, and the second was the regex. I replace the Blade::createMatcher() function by Blade::createPlainMatcher() one's and now it works fine ! – Fractaliste Feb 3 '14 at 21:41

来自  https://stackoverflow.com/questions/21532488/how-to-extend-laravel-blade-functionality-and-add-break...