2

I can define a variable using below in blade

<?php $var = var1 - var2; ?>

and then print it as

{{ $var }}

but how to set a variable using laravel blade template method and then use it, basically @set alternative in 5.2?

6
  • By using the way you said, with php tags. But can't you do it inside your controller? Commented Jul 1, 2016 at 9:01
  • yes we can do that, but do you know a way to set a variable in blade template without php tags? Commented Jul 1, 2016 at 9:03
  • 3
    why don't you do this stuff in controller, i guess controllers are for all calculation/operational stuffs and view is only for display outputs. Commented Jul 1, 2016 at 9:04
  • @Hirendrasinh S. Rathod : AGAIN yes we can do that, but do you know a way to set a variable in blade template without php tags? Commented Jul 1, 2016 at 9:06
  • no sorry i don't, you should try to ask this question on laracast forum. Commented Jul 1, 2016 at 9:07

3 Answers 3

3

There is this package for laravel 4 but it's not compatible with laravel5 due to it's composer file. A quick pull request would fix that.

The main part of the package is only three lines which could be added to your AppServiceProvider's boot method if you didn't want to pull in a package for it:

\Blade::extend(function($value, $compiler) {
    $value = preg_replace("/@set\('(.*?)'\,(.*)\)/", '<?php $$1 = $2; ?>', $value); 
    return $value;
});

Here's some examples from the readme.

@set('my_variable', $existing_variable)

You can then use the variable $my_variable in the template.


You might choose to fetch a bunch of models from your template, for example

@set('my_model_list', MyModel::where('something', '=', 1)->paginate(10))
Sign up to request clarification or add additional context in comments.

3 Comments

i know that @set works for laravel 4. but it does not work for laravel 5.2. but as you said it is simple to update, means i need to hack the package ?
I've added the relevant code to the answer, you should be able to just copy that and add it to your AppServiceProviders boot method.
You seem to be looking for a built in way to do this. Basically, there isn't because you shouldn't. You should either use a view composer, or have it processed elsewhere before it hits your view.
0

You can use blade comment syntax

{{--*/ $var = $var1 - $var2; /*--}}
{{ $var }}

Code above will be translated to following snippet

<?php /**/ $var = $var1 - $var2; /**/ ?>

Read more: http://laravel-recipes.com/recipes/256/assigning-a-variable-in-a-blade-template

4 Comments

isn't the comment syntax was {{-- This comment will not be in the rendered HTML --}}
Yes, see updated post. I was stuck finding the link :/
aah i see so this is a workaround. but i did not found this in laravel 5.2 docs.
Yeah, as you stated it -- workaround. You can take a look at the source too github.com/laravel/framework/blob/5.2/src/Illuminate/View/…
0

Try this,

@if($var=$var1-$var2)@endif

and print,

{{ $var }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.