创建一个包含刀片语法

时间:2016-02-05 23:50:57

标签: php laravel laravel-5 blade

在我看来,我有这个刀片。现在,我在视图中有6个块,因为我不确定如何重构它。

<div class="row filemanager">
<div class="col-sm-12">
  @foreach ($devices as $device)
      @if( $device->vlan_id == 100 AND $device->device_activity == 'ACTIVE' )
        <div class="col-xs-6 col-sm-4 col-md-2 text-center">
          <div class="thmb">
            <div class="btn-group fm-group" style="display: none;">
                <button type="button" class="btn btn-default dropdown-toggle fm-toggle" data-toggle="dropdown">
                  <span class="caret"></span>
                </button>
                <ul class="dropdown-menu fm-menu" role="menu">
                  <li id="device-menu">
                  <a class="changeDeviceNameBtn" href="#"><i class="fa fa-pencil"></i> Change Device Name </a>
                  </li>
                </ul>
            </div>
            <div class="thmb-prev">
              <a href="/{{$cpe_mac}}/device/{{$device->device_mac}}">

              @if(isset($device->device_name))

              {{-- Show base on device name --}}
              <img src="/images/photos/devices/{{img($device->device_name)}}.jpg" class="draggable img-responsive" alt="">

              @else

                  {{-- No Device Name Set --}}
                  @if($device->hostname != '')

                    {{-- Show base on hostname --}}
                    <img src="/images/photos/devices/{{img($device->hostname)}}.jpg" class="draggable img-responsive" alt="">

                  @else

                    {{-- Show default --}}
                    <img src="/images/photos/devices/no-img.jpg" class="draggable img-responsive" alt="">

                  @endif


              @endif

              </a>
            </div>
            <h5 class="fm-title device_name">
              <a href="/{{$cpe_mac}}/device/{{$device->device_mac}}">
              @if($device->hostname == '')
              No Devicename
              @else
              {{ $device->hostname}}
              @endif

              </a>
            </h5>
            <h5 class="text-muted device_ip">{{$device->ip_address}}</h5>
            <h5 class="text-muted device_mac">{{$device->device_mac}}</h5>

              <?php
                  $status = ucfirst(strtolower($device->device_activity));
                  if ($status == 'Active'){
                      $color = '#1CAF9A';
                  }else{
                      $color = '#D9534F';
                  }
              ?>

              <h5>{{ $status }}
                <i class="fa fa-circle" style="color:{{$color}}; margin-left: 7px;"></i>
              </h5>


          </div>
        </div>

        @endif
      @endforeach

</div>
</div>

我想创建一个包含该刀片的函数,并且只替换我的

$device->vlan_id和我的$device->device_activity

示例,

public static deviceRow(100,ACTIVE){

... my blade ... 

}

现在,我只做了6次这个功能,而不是重复那段代码6次。

甚至可能吗?

对此的任何提示/建议将不胜感激!

2 个答案:

答案 0 :(得分:2)

您可以使用刀片制作部分内容并将变量作为参数发送:

在父视图中执行以下操作:

@foreach($somelist as $item)
  @include('view.partial', ['name' => $item->name])
@endforeach

在名为partial.blade.php的文件中,执行以下操作:

{{ $device->$name }}

这是主要想法。告诉我它是否有帮助...

答案 1 :(得分:1)

您可以创建新视图并使用它发送一些参数,同时包括:

@include('my.view', ['device' => $myDevice, 'activity' => 'ACTIVE'])

数组的键将在视图中作为变量使用。

变量$myDevice将在$device

视图中以my.view的形式提供