Skip to content

Instantly share code, notes, and snippets.

@phroggyy
Created August 5, 2015 22:17
Show Gist options
  • Save phroggyy/810f251ebccf3266a717 to your computer and use it in GitHub Desktop.
Save phroggyy/810f251ebccf3266a717 to your computer and use it in GitHub Desktop.
Declare view-specific variables on the fly with a blade directive. Use @var('variable', 'value') to use. Paste the code from this gist into a service provider.
\Blade::directive('var', function($expression) {
$segments = array_map(function($segment) {
return trim($segment);
}, explode(',', preg_replace("/[\(\)\\\"\']/", '', $expression)));
return '<?php $' . $segments[0] . " = '" . $segments[1] . "' ?>";
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment