How to extend Laravel Blade functionality and add 'break' and 'continue' support

laravel, laravel-4, laravel-blade, laravel-routing, php

Solution

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.

Problem

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?

Original source

Related problems