Skip to content

Instantly share code, notes, and snippets.

@wafs
Last active November 10, 2016 10:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wafs/9b2ac1174c2a8f14d45d to your computer and use it in GitHub Desktop.
Save wafs/9b2ac1174c2a8f14d45d to your computer and use it in GitHub Desktop.
Set variables in blade without having them echo out
<?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