framework icon indicating copy to clipboard operation
framework copied to clipboard

Assign Variable In Blade Templates

Open sineld opened this issue 12 years ago • 19 comments

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.

sineld avatar Jun 20 '14 07:06 sineld

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

Anahkiasen avatar Jun 20 '14 07:06 Anahkiasen

I do not like using brackets in my views.

sineld avatar Jun 20 '14 07:06 sineld

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.

riotCode avatar Jun 20 '14 08:06 riotCode

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

taylorotwell avatar Jun 21 '14 14:06 taylorotwell

@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!

stayallive avatar Jun 21 '14 14:06 stayallive

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.

alexdover avatar Jul 11 '14 21:07 alexdover

@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

simensen avatar Jul 31 '15 18:07 simensen

+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

malhayek2014 avatar Nov 06 '15 19:11 malhayek2014

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

Thanks!

fokosun avatar Apr 22 '16 13:04 fokosun

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

https://github.com/sineld/bladeset

sineld avatar Apr 22 '16 14:04 sineld

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

fokosun avatar Apr 22 '16 15:04 fokosun

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

alexdover avatar Apr 22 '16 20:04 alexdover

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

sineld avatar Apr 22 '16 20:04 sineld

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

munotsagar avatar Jun 09 '16 08:06 munotsagar

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

Correct ?

munotsagar avatar Jun 09 '16 08:06 munotsagar

Great, Implemented correctly and its working fine.

Thanks :)

munotsagar avatar Jun 09 '16 09:06 munotsagar

Happy to hear that @munotsagar Enjoy it.

sineld avatar Jun 09 '16 09:06 sineld

Awesome, thanks @sineld this is useful for me

amalari avatar Aug 04 '16 04:08 amalari

is this good enough?

https://github.com/laravel/framework/pull/12545/files

browner12 avatar Aug 04 '16 13:08 browner12