2

In working with laravel blade templates, what's the approved way to manage variables in output?

For example, I'm working on a view that shows upcoming chores / tasks for each farmer. The pivot table holds a due_at datetime field for the task, and I'd like to change the class of the item depending on whether it's overdue, done, etc.

@foreach ($farmer->tasks as $task)
    @if ($task->pivot->due_at) < date(now))
        $style = 'alert alert-danger';
    @elseif ($task->pivot->due_at) > date(now))
        $style = 'alert alert-success';
    @else
        $style = '';
    @endif
    <div class="list-group-item {{ $style }}">{{$task->name}} <span class="glyphicon glyphicon-calendar"> {{ $task->pivot->due_at }}</span> <span class="glyphicon glyphicon-pencil"></span><span class="glyphicon glyphicon-trash"></span></div>
@endforeach 

This example throws an error: Undefined variable: style (View: /home/vagrant/Code/app/views/farmers/show.blade.php)

I don't see an obvious way to do simple code blocks to set variables like I'd do in a "normal" PHP view to define the class to apply to the task item by doing some basic calculations on the due_at value.

Should this logic be moved to a helper function or something?

worldask
  • 1,837
  • 3
  • 22
  • 37
user101289
  • 9,888
  • 15
  • 81
  • 148

2 Answers2

2

Assume due_at is a timestamp.

@foreach ($farmer->tasks as $task)
    @if (Carbon::parse($task->pivot->due_at) < Carbon::now())
        <?php $style = 'alert alert-danger'; ?>
    @elseif (Carbon::parse($task->pivot->due_at) > Carbon::now())
        <?php $style = 'alert alert-success'; ?>
    @else
        <?php $style = ''; ?>
    @endif
    <div class="list-group-item {{ $style }}">{{$task->name}} <span class="glyphicon glyphicon-calendar"> {{ $task->pivot->due_at }}</span> <span class="glyphicon glyphicon-pencil"></span><span class="glyphicon glyphicon-trash"></span></div>
@endforeach 
Anam
  • 11,999
  • 9
  • 49
  • 63
  • It prints the $style = 'alert alert-danger'; in the HTML output. You can not declare and use the variable in blade template. –  Oct 14 '14 at 05:25
  • In my case it prints the `$style = 'alert alert-danger';` code in HTML output. Can you run your code? –  Oct 14 '14 at 05:32
  • @user101289 Follow [this](http://stackoverflow.com/questions/13002626/laravels-blade-how-can-i-set-variables-in-a-template) please: –  Oct 14 '14 at 05:33
  • @PHPWeblineindia have test it ? i think you don't know it's work. – Anam Oct 14 '14 at 05:38
  • Although variables declaring in template it not desirable. I will cancel my down vote. But @user101289 You should really follow [this](http://stackoverflow.com/questions/13002626/laravels-blade-how-can-i-set-variables-in-a-template) only to use variables in template. –  Oct 14 '14 at 05:40
  • Sometime you need that. Have you checked the methods they have proposed? they all complicated. – Anam Oct 14 '14 at 05:44
0

@Anam Your answer works, but I will use following method.

@user101289 Assuming that you have default layout and it yields content section. So to declare and use variables I would suggest you use vars section in your inner template file and declare all your variables all at once on the top. And then use it. Since we will not yield the section vars, it will not going to print it.

This will help you to keep track of variables used and its standard method to declare all variables on top and use in rest of the program:

@extends('layouts.default') /* Your default layout template file. */
@section("vars")
  {{ $yourVar = 'Your value' }}
@endsection
@section("content") /* The content section which we will print */
    // Your other HTML and FORM code and you can use variables defined in **vars** section
@endsection
@stop
  • thanks, but I don't understand how this works when the variables I need to declare are in a `foreach` block. I don't think there's an elegant way to handle this in the controller either, since I'd need to still do a `foreach` in the controller, then pass in the whole modified array to the view. – user101289 Oct 15 '14 at 05:45
  • `@extends('layouts.default')` is your default layout which you are extending. And the code in `@section("content")` is going to yield. So its better to create one new section `@section("vars")` which is not going to be printed and put the variables declaration in that area. Yes its sometimes necessary to define variables in template files for printing related tasks. –  Oct 15 '14 at 05:48
  • oh, I see what you were getting at. Thanks! – user101289 Oct 15 '14 at 05:49
  • I will as soon as I've tested it. Thanks. – user101289 Oct 15 '14 at 06:02
  • @user101289 Has it worked for you? Let me know in case of any issues. –  Oct 15 '14 at 08:24