【问题标题】:How to insert laravel blade table element with one-liner?如何使用单线插入 laravel 刀片表元素?
【发布时间】:2021-12-12 02:49:31
【问题描述】:

我想简化laravel刀片的代码,所以想一下单行插入table元素的方法。

这就是我想要实现的。(这些代码不像我期望的那样工作。这两个'@include'显示相同的元素。)

在 util/insert.blade.php 中

@section('insert')
    <tr>
        <th>
            {{$th}}
        </th>
        <td>
            <input type="text" name={{$name}} value="{{ $val }}">
        </td>
    </tr>
@show

在main.blade.php中

<table class="table">
  @include('util.insert', ['th' => 'th1', 'name' => 'name1', 'val' => 'val1'])
  @include('util.insert', ['th' => 'th2', 'name' => 'name2', 'val' => 'val2'])
</table>

你有什么好的建议吗?

【问题讨论】:

标签: php html laravel laravel-blade


【解决方案1】:

您可以使用刀片组件来实现这一点。关于如何使用它的说明可以在 Laravel 文档中找到:https://laravel.com/docs/8.x/blade#components 这样,您就可以为表头、表行等创建组件。但是我不确定我是否同意它会简化刀片代码,但这只是我的意见。

【讨论】:

    【解决方案2】:

    你可以对数据使用循环

    main.blade.php

    <table class="table">
      @include('util.insert', ['data' => [
        ['th' => 'th1', 'name' => 'name1', 'val' => 'val1'],
        ['th' => 'th2', 'name' => 'name2', 'val' => 'val2'],
      ]])
    </table>
    

    util/insert.blade.php

    @section('insert')
      @foreach($data as $row)
        <tr>
          <th>
            {{$row['th']}}
          </th>
          <td>
            <input type="text" name="{{$row['name']}}" value="{{ $row['val'] }}">
          </td>
        </tr>
      @endforeach;
    @show
    

    【讨论】:

      猜你喜欢
      • 2016-10-02
      • 2013-08-09
      • 2019-02-05
      • 2015-09-13
      • 1970-01-01
      • 2015-05-13
      • 2020-04-09
      • 2021-08-08
      • 2018-07-27
      相关资源
      最近更新 更多