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?