我需要在基于laravel的网站上编辑一小段代码,但我不熟悉laravel语法,所以我不知道如何做到这一点。 基本上,有一个foreach循环列出了新闻文章。每篇文章的内部都有一个带有“news-entry-meta”类的p标签,其中包含两个span标签,第一个显示发布日期,第二个显示标签。 我的问题是标签不应该列在单个范围内,而是应该在每个标签内列出标签“badge badge-pill badge-default”。 我该怎么做?
@foreach ($news as $n)
<article class="news-entry grid-item col-12 col-md-6 col-lg-4 {{$n->tagsList}}" data-tags="{{$n->tagsList}}">
<a href="{{URL::to('/')}}/news/{{str_slug($n->title, '-')}}" class="news-entry-featured">
<img src="{{ $n->getThumbnail(720, 360) }}" class="news-entry-featured-img img-fluid" alt="image">
</a>
<div class="news-entry-text">
<p class="news-entry-meta d-flex justify-content-end align-items-center">
<span class="mr-auto">{{ Carbon\Carbon::parse($n->created_at)->format('d M Y') }}</span>
<span class="badge badge-pill badge-default">{{$n->tagsList}}</span>
</p>
<h3 class="news-entry-title">{{$n->title}}</h3>
<p class="news-entry-excerpt">{{str_limit($n->description, 100, '...')}}</p>
<a href="{{URL::to('/')}}/news/{{str_slug($n->title, '-')}}" class="news-entry-more">read more</a>
</div>
</article>
@endforeach
答案 0 :(得分:0)
如果$n->tagList
是一个字符串,并且在评论中提到空格,那么这应该有效。
@foreach ($news as $n)
<article class="news-entry grid-item col-12 col-md-6 col-lg-4 {{$n->tagsList}}" data-tags="{{$n->tagsList}}">
<a href="{{URL::to('/')}}/news/{{str_slug($n->title, '-')}}" class="news-entry-featured">
<img src="{{ $n->getThumbnail(720, 360) }}" class="news-entry-featured-img img-fluid" alt="image">
</a>
<div class="news-entry-text">
<p class="news-entry-meta d-flex justify-content-end align-items-center">
<span class="mr-auto">{{ Carbon\Carbon::parse($n->created_at)->format('d M Y') }}</span>
@foreach(explode(' ',$n->tagsList) as $tag)
<span class="badge badge-pill badge-default">{{$tag}}</span>
@endforeach
</p>
<h3 class="news-entry-title">{{$n->title}}</h3>
<p class="news-entry-excerpt">{{str_limit($n->description, 100, '...')}}</p>
<a href="{{URL::to('/')}}/news/{{str_slug($n->title, '-')}}" class="news-entry-more">read more</a>
</div>
</article>
@endforeach