在@foreach循环中显示一个@section

问题描述:

您好我是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 

因为你只想要显示它曾经

入住这里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 

文档,你可以添加一个简单的变量把一个检查,如果一个节被你应在使用别的之前显示或不显示。

$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 

不知道这是最优雅的解决方案与否。但你可以达到你想要的如下:

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