doupin1073 2017-05-16 00:39
浏览 16
已采纳

Laravel:如何在自己的范围内列出每个帖子标签?

I need to edit a tiny piece of code on a laravel based website, but I'm not familiar with laravel syntax, so I don't know how to do this. Basically, there is a foreach loop that lists news articles. Inside of each article there is a p tag with a class "news-entry-meta", which contains two span tags, the first one displaying a post date, and the second one listing tags. My problem is that tags should not be listed inside a single span, but instead each tag should be listed inside its own span with classes "badge badge-pill badge-default". How do I do this?

    @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
  • 写回答

1条回答 默认 最新

  • dongmei425373 2017-05-16 01:31
    关注

    this should work if $n->tagList is a string with spaces between as you mentioned in a comment.

      @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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记