Last active
November 10, 2016 10:49
-
-
Save wafs/9b2ac1174c2a8f14d45d to your computer and use it in GitHub Desktop.
Set variables in blade without having them echo out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Providers; | |
use Illuminate\Support\Facades\Blade; | |
use Illuminate\Support\ServiceProvider; | |
class BladeExtensionServiceProvider extends ServiceProvider { | |
/** | |
* Bootstrap the application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
Blade::extend(function($view, $compiler) { | |
/* | |
* Use like : | |
* @setvar('var', 'value') | |
* @setvar('var', [0,2,1,5,2]) | |
* @setvar('a', 10) | |
* @setvar('b', 20) | |
* @setvar('a' $b) // $a now has $b's value | |
* | |
*/ | |
$pattern = '!\@setvar\(\s*([^,]+)\s*,\s*(.+)\s*\)!'; | |
return preg_replace($pattern, '<?php ${$1} = $2 ?>', $view); | |
}); | |
} | |
/** | |
* Register the application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment