简体   繁体   English

laravel刀片模板变量

[英]laravel blade template variables

In working with laravel blade templates, what's the approved way to manage variables in output? 在使用laravel刀片模板时,已批准的管理输出变量的方法是什么?

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. 数据透视表包含任务的due_at datetime字段,我想根据项目的过期,完成等来更改项目的类。

@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) 此示例引发错误: 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. 我没有看到一种简单的代码块来设置变量的明显方法,就像我在“正常” PHP视图中那样通过对due_at值进行一些基本计算来定义要应用于任务项的类due_at

Should this logic be moved to a helper function or something? 是否应将此逻辑移至辅助功能或其他功能?

Assume due_at is a timestamp. 假设due_at是一个时间戳。

@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 Your answer works, but I will use following method. @Anam您的答案有效,但是我将使用以下方法。

@user101289 Assuming that you have default layout and it yields content section. @ user101289假定您具有默认布局,并且将产生内容部分。 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. 因此,要声明和使用变量,我建议您在内部模板文件中使用vars部分,并一次在顶部声明所有变量。 And then use it. 然后使用它。 Since we will not yield the section vars , it will not going to print it. 由于我们不会产生vars节,因此不会打印它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM