2017-06-15 269 views
0

您好我是laravel的新手,無法理解sections,但我試圖在循環中只顯示一次特定的一次,然後重複其他所有操作,如果一定的條件是真的。這是我想修改的代碼。在這裏,我評論了我只想要一次的那一行。在@foreach循環中顯示一個@section

@foreach ($pageTypes['news-events']->categories as $cat) 
    <?php $related = $page->relatedPages($pageTypes['news-events']->id, $cat->id); ?> 
    @if ($related->count() > 0) 
     @section('tab-titles') 
     //i want to display this LI once if true 
     <li class="tab first"><a href="#news" title="insight">Insight</a></li> 
     @append 
     @section('news-content') 
      <div id="news" class="tabs_text"> 
       <h2>{{ strtoupper($cat->name) }} 
       </h2> 
       <ul> 
       @foreach($related as $news) 
        <li><a href="{{ url($news->url) }}" title="{{ $news->title }}"> 
          {{ $news->title }} 
         </a> 
        </li> 
       @endforeach 
       </ul> 
      </div> 
     @append 
    @endif 
    @endforeach 

回答

1

因爲你只想要顯示它曾經

入住這裏link

@if ($related->count() > 0) 
    @section('news-content') 
     <div id="news" class="tabs_text"> 
      <h2>{{ strtoupper($cat->name) }} 
      </h2> 
      <ul> 
      @foreach($related as $news) 
       <li><a href="{{ url($news->url) }}" title="{{ $news->title }}"> 
         {{ $news->title }} 
        </a> 
       </li> 
      @endforeach 
      </ul> 
     </div> 
    @append 
@else 
    @section('tab-titles') 
    //i want to display this LI once if true 
    <li class="tab first"><a href="#news" title="insight">Insight</a></li> 
    @append 
@endif 
0

文檔,你可以添加一個簡單的變量把一個檢查,如果一個節被你應在使用別的之前顯示或不顯示。

$check = 0; 
@foreach ($pageTypes['news-events']->categories as $cat) 
     <?php $related = $page->relatedPages($pageTypes['news-events']->id, $cat->id); ?> 

     @if ($related->count() > 0) 


      if($check==0) 
    { 
      @section('tab-titles') 
    //i want to display this LI once if true 
     <li class="tab first"><a href="#news" title="insight">Insight</a></li> 
      @append 
    $check++; 
    } 
      @section('news-content') 
       <div id="news" class="tabs_text"> 
        <h2>{{ strtoupper($cat->name) }} 
        </h2> 
        <ul> 
        @foreach($related as $news) 
         <li><a href="{{ url($news->url) }}" title="{{ $news->title }}"> 
           {{ $news->title }} 
          </a> 
         </li> 
        @endforeach 
        </ul> 
       </div> 
      @append 
     @endif 
     @endforeach 
0

不知道這是最優雅的解決方案與否。但你可以達到你想要的如下:

<?php $repeat = true; // first initialize as true ?> 
@foreach ($pageTypes['news-events']->categories as $cat) 
    <?php $related = $page->relatedPages($pageTypes['news-events']->id, $cat->id); 

?> 

    @if ($related->count() > 0) 
     @if($your_certain_condition == true && $repeat) 
     <?php $repeat = false; ?> 
     @section('tab-titles') 
     <li class="tab first"><a href="#news" title="insight">Insight</a></li> 
     @append 
     @endif 

     @section('news-content') 
      <div id="news" class="tabs_text"> 
       <h2>{{ strtoupper($cat->name) }} 
       </h2> 
       <ul> 
       @foreach($related as $news) 
        <li><a href="{{ url($news->url) }}" title="{{ $news->title }}"> 
          {{ $news->title }} 
         </a> 
        </li> 
       @endforeach 
       </ul> 
      </div> 
     @append 
    @endif 
    @endforeach