Search code examples
phplaravellaravel-5laravel-blade

Make a function containing blade syntax


I have this blade in my view. Right now, I have 6 blocks of them in my view because I'm not sure how to refactor it.

<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>

I want to make a function containing that blade, and only replace my

$device->vlan_id, and my $device->device_activity.

Example,

public static deviceRow(100,ACTIVE){

... my blade ... 

}

Now, I just that function 6 times, rather than duplicate that block of code 6 times.

Is it even possible?


Solution

  • You can make a partial with your blade and send a variable as a parameter:

    In your parent view do something like this:

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

    And in a file called partial.blade.php, do something like this:

    {{ $device->$name }}
    

    It's the main idea. Tell me if it helps...