删除刀片模板foreach中的空ul

时间:2017-03-22 08:15:58

标签: php html laravel-blade

我在刀片中有foreach循环,我在其中显示类别和子类别。这是刀片的一部分

<div id="sidebar">
   <h1>Categories</h1>
      <ul class="nav nav-stacked cat-nav">
      @foreach($menu as $category_menu)
         @if($menu->has('subcategories'))    
             <li>
                <a href="#">{{ $menu['category_name'] }}</a>
                 <ul>
                    @if($menu->subcategories->count())                 
                       @foreach($menu->subcategories as $subcategory)
                          @if($subcategory->products->count())
                              <li><a href="#">{{$subcategory->sub_cat_name}}</a></li>
                          @endif
                       @endforeach                      
                    @endif
                 </ul>
             </li>
             @else
             <li><a href="#"><i class="fa fa-link"></i> <span>{{ $menu->category_name }}</span></a></li>
         @endif          
      @endforeach
    </ul>
</div>

这创建了下面的HTML

<ul class="nav nav-stacked cat-nav">                                  
    <li>
        <a href="#">Category_1</a>
           <ul>
               <li>
                   <a href="#">Sub Category in Category_1</a>
               </li>                                                    
           </ul>
    </li>                                                                 
    <li>
        <a href="#">Category_2</a>
           <ul>
           </ul>
    </li>                    
</ul>

您可以在ul下看到空的Category_2,因为Category_2没有任何已分配的子类别ul元素为空。也许是很容易修复,但我试图玩循环,但无法解决它。

3 个答案:

答案 0 :(得分:2)

您必须在ul条件下移动if,例如:

@if($menu->subcategories->count())                 
<ul>
   @foreach($menu->subcategories as $subcategory)
      @if($subcategory->products->count())
          <li><a href="#">{{$subcategory->sub_cat_name}}</a></li>
      @endif
   @endforeach                      
</ul>
@endif

当存在某些数据时生成ul

Edit:如果仍然无效,请从

更改条件
$menu->subcategories->count() 

count($menu->subcategories) > 0

答案 1 :(得分:1)

如果<ul>已被声明,则必须将@if($subcategory->products->count())置于<ul></ul>内,以防止分隔</ul>中的所有列表。然后在循环后添加@if($menu->subcategories->count()) @foreach($menu->subcategories as $subcategory) <?php $ul = false; //Create a temporary variable to validate if <ul> is already declared ?> @if($subcategory->products->count()) @if(!$ul) <?php $ul = true; ?> <ul> @endif <li><a href="#">{{$subcategory->sub_cat_name}}</a></li> @if($ul) </ul> <!-- End ul here --> @endif @endif @endforeach @endif 条件。

<OTA_AirLowFareSearchRQ AvailableFlightsOnly="true" ResponseType="OTA" ResponseVersion="3.0.0" Version="3.0.0" xmlns="http://www.opentravel.org/OTA/2003/05">
<POS xmlns="http://www.opentravel.org/OTA/2003/05">
    <Source PseudoCityCode="XXXX">
        <RequestorID ID="REQ.ID" Type="0.AAA.X">
            <CompanyName Code="TN"/>
        </RequestorID>
    </Source>
</POS>
<OriginDestinationInformation RPH="1">
    <DepartureDateTime>2017-04-21T00:00:00</DepartureDateTime>
    <DepartureWindow>00002359</DepartureWindow>
    <OriginLocation LocationCode="YYZ" LocationType="A"/>
    <DestinationLocation LocationCode="SIN" LocationType="A"/>
    <TPA_Extensions>
        <SegmentType Code="O"/>
    </TPA_Extensions>
</OriginDestinationInformation>
<TravelPreferences ETicketDesired="true" ValidInterlineTicket="true">
    <TPA_Extensions>
        <TripType Value="OneWay"/>
        <FlightStopsAsConnections Ind="true"/>
        <ExcludeCallDirectCarriers Enabled="true"/>
    </TPA_Extensions>
    <Baggage Description="true" RequestType="A"/>
</TravelPreferences>
<TravelerInfoSummary>
    <SeatsRequested>1</SeatsRequested>
    <AirTravelerAvail>
        <PassengerTypeQuantity Code="ADT" Quantity="1"/>
    </AirTravelerAvail>
    <PriceRequestInformation CurrencyCode="TRY" NegotiatedFaresOnly="false"/>
</TravelerInfoSummary>
<TPA_Extensions>
    <IntelliSellTransaction>
        <RequestType Name="50ITINS"/>
    </IntelliSellTransaction>
</TPA_Extensions></OTA_AirLowFareSearchRQ>

答案 2 :(得分:1)

在控制器中,您可以向$menu添加一个新属性,该属性将保留一个布尔值,无论任何子类别是否包含产品:

$menu->subcategoriesHaveProducts = $menu->subcategories->map(function($subcategory) use($menu) {
    return !$subcategory->products->isEmpty() || $menu->subcategoriesHaveProducts;
});

之后你必须改变if条件以检查菜单是否有任何子类别:

@foreach($menu as $category_menu)
     @if($menu->has('subcategories'))    
         <li>
            <a href="#">{{ $menu['category_name'] }}</a>
              @if($menu->subcategories->count() && $menu->subcategoriesHaveProducts)

              <ul>
                   @foreach($menu->subcategories as $subcategory)
                      @if($subcategory->products->count())
                          <li><a href="#">{{$subcategory->sub_cat_name}}</a></li>
                      @endif
                   @endforeach
             </ul>                      
             @endif
         </li>
         @else
         <li><a href="#"><i class="fa fa-link"></i> <span>{{ $menu->category_name }}</span></a></li>
     @endif          
  @endforeach