338

I'm reading the Laravel Blade documentation and I can't figure out how to assign variables inside a template for use later. I can't do {{ $old_section = "whatever" }} because that will echo "whatever" and I don't want that.

I understand that I can do <?php $old_section = "whatever"; ?>, but that's not elegant.

Is there a better, elegant way to do that in a Blade template?

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
duality_
  • 17,738
  • 23
  • 77
  • 95

31 Answers31

482

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://stackoverflow.com/a/28641054/2169147 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.

Pim
  • 5,626
  • 4
  • 19
  • 27
  • 7
    Nice! Please note that you can execute any php statement with your implementation. I would rename it to soemething like @php. Very handy... – igaster Jan 09 '15 at 08:35
  • Very true, igaster. You can rename 'define' to 'php' if you want, but that opens the pitfall to over-using php in your templates :) – Pim Mar 10 '15 at 10:19
  • I'm currently running the LTS (5.1) version. I had to add `\ ` before Blade::extend... That is explained in the given link tho. But I thought it's worth to mention. Absolute elegant solution btw. `+1` ! – Carsten Jun 08 '16 at 14:20
  • 1
    Thanks @C.delaFonteijne, if you are using namespacing (and you should), the \ is indeed needed. I've added the \ in the code above. – Pim Jun 08 '16 at 18:11
  • 1
    Please note that almost your exact implementation is standard [since Laravel 5.2](https://github.com/illuminate/view/blob/5.2/Compilers/BladeCompiler.php#L797-L819). You can use `@php(@i = 1)` or use it in a block statement (close with `@endphp`) – Daan Jul 21 '16 at 09:25
  • Awsome {{-- */$i=0;/* --}} use to work with laravel 5.2 does not in 5.3 @php ($i = 1) works perfectly in 5.3 – Louwki Sep 20 '16 at 09:39
  • 2
    I don't see how "@php" "@endphp" is "more elegant" than "". It's even a few characters longer! Is it just because it starts with a "@" like other Blade directives? We developers are some obsessive-compulsive bunch! ;-) – OMA Mar 19 '19 at 09:32
  • What is meant with your first sentence: The @php blade directive no longer accepts inline tags. Instead, use the full form of the directive ... Does that meant that `@php ($i = 1)` isn't valid anymore or am I misinterpreting this? – ralphjsmit Apr 05 '21 at 12:06
  • @ralphjsmit indeed `@php ($i = 1)` should not be working anymore, although I have not tested this in the newest versions. You should now use: `@php $i = 1 @endphp` – Pim Apr 06 '21 at 07:45
  • @Pim Ok thanks for confirming! I just tried it and the old syntax does still work (Laravel 8.33.1). Anyway, it's more future proof to use the new one. – ralphjsmit Apr 07 '21 at 15:52
137

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);
});
TLGreg
  • 8,321
  • 3
  • 23
  • 12
  • 11
    Variables in views have some uses. This looks great! Where would be a good place to put this code? – duality_ Oct 23 '12 at 11:01
  • 1
    You can put it in your application/start.php or if you will have more things like this put it in a separate file and include it there. Laravel is very loose in this way, you could even put thin a controller. The only thing you have to do these extends before the view is rendered. – TLGreg Oct 24 '12 at 00:37
  • For LARAVEL 4 add this code to the end of the global.php file in the app/start – JulianoMartins Mar 18 '14 at 13:29
  • 22
    What is the reasoning for adding this extra code just to use `{?` instead of just using the native `` ? – Justin Jul 28 '14 at 17:36
  • 1
    If it is discouraged to do, is there a more "proper" way to do the following? I have a site where the title is rendered in the main app view as {{ $title }}, which contains a subsystem who needs to append the page number to the title ("Application Form Page {{ $page }}") and I am passing $page to the view (which is used otherwise within the view). I don't want to build the title in each controller call, I just want to send the view the page number - just in case some day I want to change the base title. I'm using now, but is there a more correct way? – jdavidbakr Apr 24 '15 at 17:19
  • 5
    Variables should be passed from the controller, not declared inline in your view. If a global template needs a variable, you can set it in a service provider http://stackoverflow.com/a/36780419/922522. If a page specific template needs a variable, use @yield and pass it from the child view that has a controller. https://laravel.com/docs/5.1/blade#template-inheritance – Justin May 10 '16 at 16:26
  • 1
    Laravel's manual points out that you can use PHP in blade templates as one of blade's strenghts. "Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views." - second sentence in the docs. I go for PHP tags. Pretty enough and absolutely easy to understand for everyone. – Hop hop Dec 24 '17 at 05:52
  • Use `@php` and `@endphp` tags built into blade. Using this answer does not leave the variable set in the blade template in laravel 5.3+ – dansch Feb 05 '19 at 16:35
119

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.

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
Trying Tobemyself
  • 3,668
  • 3
  • 28
  • 43
  • 3
    @trying-tobemyself +1 | Not the *Best-Practices* way, but _perfect_ for quick hacking of code in templates like with _inline styles_ for html. – Markus Hofmann Sep 28 '13 at 12:44
  • 137
    I wouldn't recommend doing this hack as anyone who looks at this code after you is going to hate you. – Justin Feb 06 '14 at 20:54
  • 2
    Agree with Justin, comment tags are for comments, to uncomment within the comment and start doing something else is asking for trouble – Leon Apr 10 '14 at 10:20
  • 1
    I find this useful for debugging when stepping through a template in PHPStorm – pymarco Jul 10 '14 at 13:32
  • 28
    This is not better than plain ol' php `` – gyo Sep 25 '14 at 09:44
  • This could win a code obfuscation contest over on http://codegolf.stackexchange.com/ – Tobia Apr 20 '16 at 14:50
  • 3
    What's the point of doing this instead of using php tags?? It's not readable (looks like a comment), requires more typing, and may get broken with an update to the parsing system. Even if it has a point, it's not the answer to how to define a variable in a blade template. Don't know what's wrong with the voters, perhaps they found this so 'geeky'? meh... – DarkWingDuck May 24 '16 at 14:48
  • this like hack life :) – Abdul Aziz Al Basyir Jun 10 '19 at 03:18
62

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.

iconoclast
  • 21,213
  • 15
  • 102
  • 138
BTMPL
  • 1,581
  • 13
  • 12
39

Ya'll are making it too complicated.

Just use plain php

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

donesies.

(or https://github.com/alexdover/blade-set 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)

Sabrina Leggett
  • 9,079
  • 7
  • 47
  • 50
28

Since Laravel 5.2.23, you have the @php Blade directive, which you can use inline or as block statement:

@php($old_section = "whatever")

or

@php
    $old_section = "whatever"
@endphp
Terrence
  • 194
  • 1
  • 12
Daan
  • 7,685
  • 5
  • 43
  • 52
23

You Can Set Variables In The Blade Templating Engine The Following Ways:

1. General PHP Block
Setting Variable: <?php $hello = "Hello World!"; ?>
Output: {{$hello}}

2. Blade PHP Block
Setting Variable: @php $hello = "Hello World!"; @endphp
Output: {{$hello}}

Prince Ahmed
  • 1,038
  • 10
  • 10
20

You can set a variable in the view file, but it will be printed just as you set it. Anyway, there is a workaround. You can set the variable inside an unused section. Example:

@section('someSection')
  {{ $yourVar = 'Your value' }}
@endsection

Then {{ $yourVar }} will print Your value anywhere you want it to, but you don't get the output when you save the variable.

EDIT: naming the section is required otherwise an exception will be thrown.

Yidir
  • 583
  • 7
  • 12
Vasile Goian
  • 425
  • 5
  • 9
  • Doesn't work something else need to be included Undefined property: Illuminate\View\Factory::$startSection (View: /home/vagrant/Code/dompetspy/resources/views/reviews/index.blade.php) – MaXi32 Sep 03 '15 at 17:53
  • Actually that's very smart! Just call your section 'variables' and use it strictly for assigning variables in blade and voila! – Lamar Aug 16 '20 at 12:21
19

In laravel document https://laravel.com/docs/5.8/blade#php You can do this way:

@php
     $my_variable = 123;
@endphp
Hamidreza
  • 1,825
  • 4
  • 29
  • 40
17

In Laravel 4:

If you wanted the variable accessible in all your views, not just your template, View::share is a great method (more info on this blog).

Just add the following in app/controllers/BaseController.php

class BaseController extends Controller
{
  public function __construct()
  {                   
    // Share a var with all views
    View::share('myvar', 'some value');
  }
}

and now $myvar will be available to all your views -- including your template.

I used this to set environment specific asset URLs for my images.

Community
  • 1
  • 1
Justin
  • 26,443
  • 16
  • 111
  • 128
9

Laravel 7 :

{{ $solution = "Laravel 7 is awesome and easy to use !!" }}
Hicham O-Sfh
  • 731
  • 10
  • 12
8

And suddenly nothing will appear. From my experience, if you have to do something like this prepare the html in a model's method or do some reorganizing of your code in to arrays or something.

There is never just 1 way.

{{ $x = 1 ? '' : '' }}
Michael J. Calkins
  • 32,082
  • 15
  • 62
  • 91
  • 12
    Prepare the HTML in the model? That's the ugliest thing imaginable. – duality_ Mar 27 '13 at 15:56
  • @duality_ You're declaring and changing variables in your view. I said you're probably organizing your code wrong. Lrn 2 architect. – Michael J. Calkins Mar 27 '13 at 23:44
  • 3
    Sure thing, Michael... Those variables are not variables such as `$users = ...`, but something along the lines `$css_class = ...`, so strictly design variables that don't belong to the model or controller, as they are determined by the designer. – duality_ Mar 28 '13 at 07:33
  • 2
    if you need to go that route, I prefer the more simple and elegant solution: {{ ''; $x = 1 }} – Daniel Fanica Sep 30 '14 at 21:08
7

In Laravel 5.1, 5.2:

https://laravel.com/docs/5.2/views#sharing-data-with-all-views

You may need to share a piece of data with all views that are rendered by your application. You may do so using the view factory's share method. Typically, you should place calls to share within a service provider's boot method. You are free to add them to the AppServiceProvider or generate a separate service provider to house them.

Edit file: /app/Providers/AppServiceProvider.php

<?php

namespace App\Providers;

class AppServiceProvider extends ServiceProvider
{        
    public function boot()
    {
        view()->share('key', 'value');
    }

    public function register()
    {
        // ...
    }
}
Justin
  • 26,443
  • 16
  • 111
  • 128
7

I'm going to extend the answer given by @Pim.

Add this to the boot method of your AppServiceProvider

<?php
/*
|--------------------------------------------------------------------------
| Extend blade so we can define a variable
| <code>
| @set(name, value)
| </code>
|--------------------------------------------------------------------------
*/

Blade::directive('set', function($expression) {
    list($name, $val) = explode(',', $expression);
    return "<?php {$name} = {$val}; ?>";
});

This way you don't expose the ability to write any php expression.

You can use this directive like:

@set($var, 10)
@set($var2, 'some string')
Carson
  • 432
  • 6
  • 17
6

You may use the package I have published: https://github.com/sineld/bladeset

Then you easily set your variable:

@set('myVariable', $existing_variable)

// or

@set("myVariable", "Hello, World!")
Sinan Eldem
  • 5,564
  • 3
  • 36
  • 37
4

As for my elegant way is like the following

{{ ''; $old_section = "whatever"; }}

And just echo your $old_section variable.

{{ $old_section }}
Set Kyar Wa Lar
  • 4,488
  • 4
  • 30
  • 57
4

If you have PHP 7.0:

The simple and most effective way is with assignment inside brackets.

The rule is simple: Do you use your variable more than once? Then declare it the first time it's used within brackets, keep calm and carry on.

@if(($users = User::all())->count())
  @foreach($users as $user)
    {{ $user->name }}
  @endforeach
@else
  There are no users.
@endif

And yes, I know about @forelse, this is just a demo.

Since your variables are now declared as and when they are used, there is no need for any blade workarounds.

Jonathan
  • 10,936
  • 8
  • 64
  • 79
4

Assign variable to the blade template, Here are the solutions

We can use <?php ?> tag in blade page

<?php $var = 'test'; ?>
{{ $var }

OR

We can use the blade comment with special syntax

{{--*/ $var = 'test' /*--}}
{{ $var }}
Nikunj K.
  • 8,779
  • 4
  • 43
  • 53
4

I also struggled with this same issue. But I was able to manage this problem by using following code segment. Use this in your blade template.

<input type="hidden" value="{{$old_section = "whatever" }}">

{{$old_section }}
Pasindu
  • 41
  • 3
2

I don't think that you can - but then again, this kind of logic should probably be handled in your controller and passed into the view already set.

Darren Craig
  • 462
  • 3
  • 8
  • 7
    Some variables are strictly for views. `$previous_group_name`, `$separator_printed`, etc. – duality_ Oct 22 '12 at 00:22
  • 2
    If it is only for views, you should just pass it to the view from the controller. If you want it available to all views, see my answer above using `app/controllers/BaseController.php`. – Justin Feb 06 '14 at 20:56
  • 1
    I'm using multiple arrays to send all my $data to the view – mercury Jun 02 '16 at 23:57
2

I was looking for a way to assign a value to a key and use it many times in my view. For this case, you can use @section{"key", "value"} in the first place and then call @yield{"key"} to output the value in other places in your view or its child.

Hafez Divandari
  • 8,381
  • 4
  • 46
  • 63
2

In laravel8

@php

 $name="Abdul mateen";

{{ echo $name; }}
    
@endphp
Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
1

Hacking comments is not a very readable way to do it. Also editors will color it as a comment and someone may miss it when looking through the code.

Try something like this:

{{ ''; $hello = 'world' }}

It will compile into:

<?php echo ''; $hello = 'world'; ?>

...and do the assignment and not echo anything.

Howard
  • 3,648
  • 13
  • 58
  • 86
1

It's better to practice to define variable in Controller and then pass to view using compact() or ->with() method.

Otherwise #TLGreg gave best answer.

Mandeep Gill
  • 4,577
  • 1
  • 28
  • 34
1

There is a very good extention for Blade radic/blade-extensions. After you add it you can use @set(variable_name, variable_value)

@set(var, 33)
{{$var}}
yanko-belov
  • 503
  • 2
  • 6
  • 15
0

In my opinion it would be better to keep the logic in the controller and pass it to the view to use. This can be done one of two ways using the 'View::make' method. I am currently using Laravel 3 but I am pretty sure that it is the same way in Laravel 4.

public function action_hello($userName)
{
    return View::make('hello')->with('name', $userName);
}

or

public function action_hello($first, $last)
{
    $data = array(
        'forename'  => $first,
        'surname' => $last
    );
    return View::make('hello', $data);
}

The 'with' method is chainable. You would then use the above like so:

<p>Hello {{$name}}</p>

More information here:

http://three.laravel.com/docs/views

http://codehappy.daylerees.com/using-controllers

Robert Brisita
  • 5,461
  • 3
  • 36
  • 35
  • Presentation logic is best kept in the view. Sometimes, you need to create a variable from within the view. e.g. to format a date. `$format='Y-m-d H:i:s';` that way you can re-use that format within the view. This certainly does not belong in the controller. That said, in response to the question... There is nothing wrong with `` tags. – Gravy Jan 24 '14 at 13:12
0

I had a similar question and found what I think to be the correct solution with View Composers

View Composers allow you to set variables every time a certain view is called, and they can be specific views, or entire view templates. Anyway, I know it's not a direct answer to the question (and 2 years too late) but it seems like a more graceful solution than setting variables within a view with blade.

View::composer(array('AdminViewPath', 'LoginView/subview'), function($view) {
    $view->with(array('bodyClass' => 'admin'));
});
dug
  • 340
  • 3
  • 7
0

laravel 5 you can easily do this . see below

{{--*/ @$variable_name = 'value'  /*--}}
tapos ghosh
  • 2,114
  • 23
  • 37
0

You can extend blade by using the extend method as shown below..

Blade::extend(function($value) {
    return preg_replace('/\@var(.+)/', '<?php ${1}; ?>', $value);
});

after that initialize variables as follows.

@var $var = "var"
0

inside the blade file, you can use this format

@php
  $i++
@endphp
-2

works in all versions of blade.

{{--*/  $optionsArray = ['A', 'B', 'C', 'D','E','F','G','H','J','K'] /*--}}
Bakhtawar GIll
  • 407
  • 5
  • 16