Framework: Assign Variable In Blade Templates

Created on 20 Jun 2014  路  19Comments  路  Source: laravel/framework

Hi,

It is possible to extend blade and assign variables like:

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

or

{{--*/ $var = 'test' /*--}}

but a clearer way would look more laravelish:

{{ var('variable', 'value') }}

Regards.

Most helpful comment

I wanted to achieve a similar thing and ended up writing a package which will allow you to do

@set('variable_name', $value)

It is here https://github.com/alexdover/blade-set if that helps, at least in the meantime.

Personally, I agree with @Stayallive - there are some use cases where setting/assigning variables in a view is appropriate.

All 19 comments

Well or you can do <?php $old_section = 'whatever' ?> that's basically shorter than all the above no?

I do not like using brackets in my views.

There's very little cases I can think of that assigning a variable within the view is acceptable, I don't think it's good to implement something that would encourage to do so. In MVC if you want to assign a value to a variable it should be done before and passed to the view.

Feel free to submit a pull request on this repository for it.

@riotCode I use it in some cases where I require some math or a custom loop to render my views correctly. So technically this is only needed in the view and I am not assigning/setting/calculating those kind of things in a controller obviously.

This gets a big :+1: from me!

I wanted to achieve a similar thing and ended up writing a package which will allow you to do

@set('variable_name', $value)

It is here https://github.com/alexdover/blade-set if that helps, at least in the meantime.

Personally, I agree with @Stayallive - there are some use cases where setting/assigning variables in a view is appropriate.

@taylorotwell Would you consider someone implementing @alexdover's set directly in blade? I like that syntax a little more than the variations shown in this issue. I'd like to take a crack at implementing it but over here you said to just use <?php $foo = 'bar'; ?>. (that was a year earlier, though)

https://github.com/alexdover/blade-set

An example where setting and keeping track of a variable inside a template using this sytax would be processing a list of things where each thing has a week and you want to set a week header for each group of weeks:

@set('week', 0);

@foreach ($things as $thing)
  @if ($week != $thing->week)
    WEEK {{ $thing->week }}
    @set('week', $thing->week)
  @endif

  Title: {{ $thing->title }}
@endforeach

+1 I think a function to set a variable is useful. I had to write some code in my partial view to determine the value of the input I could not use blade pure for that rather had to use PHP code like so

        <?php

            if( ! $errors->isEmpty() ){

                $value = Input::old($controlName);

            } elseif( isset( $answer ) ){

                $value = $answer;

            } else {

                $value = '';

            }

        ?>

Perhaps this would be a handy little feature in Laravel 5.2

@alexdover have u got a version for laravel 5? I would love that!

Thanks!

@andela-fokosun I have written a package for that and yet it is for Laravel 5.

https://github.com/sineld/bladeset

wow thank you @sineld this is soooo cool, thanks again

@sineld Nice one, I was thinking of updating it the other day, but looks like you've got it covered!

Thank you @alexdover You have made it useful, I have just updated it.

Facing some issues while installing this package into new version of laravel

to install this command would be : composer require sineld/bladeset

Correct ?

Great, Implemented correctly and its working fine.

Thanks :)

Happy to hear that @munotsagar Enjoy it.

Awesome, thanks @sineld this is useful for me

Was this page helpful?
0 / 5 - 0 ratings