嗨,我是laravel的新手并且不能很好地理解sections
,但是我试图在一个循环中只显示一个特定的一个,然后重复其他一切,如果某个条件为真。这是我试图修改的代码。在这里,我评论了我只想要一次的线。
@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
答案 0 :(得分:1)
你应该使用else,因为你只想显示一次
在此处查看文档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
答案 1 :(得分: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
答案 2 :(得分: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