php - How to Set Variables in a Laravel Blade Template

ID : 10083

viewed : 63

Tags : phplaravellaravel-4laravel-bladephp

Top 5 Answer for php - How to Set Variables in a Laravel Blade Template

vote vote



Use the full form of the blade directive:

@php $i = 1 @endphp 

LARAVEL 5.2 - 5.4

You can use the inline tags:

@php ($i = 1) 

Or you can use it in a block statement:

@php $i = 1 @endphp 


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 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 :)


You can just put the above code on the bottom of app/start/global.php (or any other place if you feel that is better).

After the above changes, you can use:

@define $i = 1 

to define a variable.

vote vote


It is discouraged to do in a view so there is no blade tag for it. If you do want to do this in your blade view, you can either just open a php tag as you wrote it or register a new blade tag. Just an example:

<?php /**  * <code>  * {? $old_section = "whatever" ?}  * </code>  */ Blade::extend(function($value) {     return preg_replace('/\{\?(.+)\?\}/', '<?php ${1} ?>', $value); }); 
vote vote


In , you can use the template comment syntax to define/set variables.

Comment syntax is {{-- anything here is comment --}} and it is rendered by engine as

<?php /* anything here is comment */ ?>

so with little trick we can use it to define variables, for example

{{-- */$i=0;/* --}} 

will be rendered by as <?php /* */$i=0;/* */ ?> which sets the variable for us. Without changing any line of code.

vote vote


There is a simple workaround that doesn't require you to change any code, and it works in Laravel 4 just as well.

You just use an assignment operator (=) in the expression passed to an @if statement, instead of (for instance) an operator such as ==.

@if ($variable = 'any data, be it string, variable or OOP') @endif 

Then you can use it anywhere you can use any other variable

{{ $variable }} 

The only downside is your assignment will look like a mistake to someone not aware that you're doing this as a workaround.

vote vote


Ya'll are making it too complicated.

Just use plain php

<?php $i = 1; ?> {{$i}} 


(or looks pretty straighforward too)

We're all kinda "hacking" the system by setting variables in views, so why make the "hack" more complicated then it needs to be?

Tested in Laravel 4.

Another benefit is that syntax highlighting works properly (I was using comment hack before and it was awful to read)

Top 3 video Explaining php - How to Set Variables in a Laravel Blade Template