EASY WAY
If you want to define multiple variables, use the full form of the blade directive:
@php
$i = 1;
$j = 2;
@endphp
If you only want to define one variable, you can also use a single PHP statement:
@php($i = 1)
MORE ADVANCED: ADD A 'DEFINE' TAG
If you want to use custom tags and use a @define instead of @php, extend Blade like this:
/*
|--------------------------------------------------------------------------
| Extend blade so we can define a variable
| <code>
| @define $variable = "whatever"
| </code>
|--------------------------------------------------------------------------
*/
\Blade::extend(function($value) {
return preg_replace('/\@define(.+)/', '<?php ${1}; ?>', $value);
});
Then do one of the following:
Quick solution: If you are lazy, just put the code in the boot() function of the AppServiceProvider.php.
Nicer solution:
Create an own service provider. See https://mcmap.net/q/94260/-where-to-place-blade-extend on how to extend blade in Laravel 5. It's a bit more work this way, but a good exercise on how to use Providers :)
After the above changes, you can use:
@define $i = 1
to define a variable.
<?php $old_section = "whatever"; ?>
. I find it quite readable. – Disfrock