為了方便程式碼的管理,有時候我們會使用:
@foreach ($topics as $topic)
@include('topics._topic_simple', ['topic' => $topic])
@endforeach
在上面的例子中,每一個 topics._topic_simple
對應下圖的每一個文章元素:
今天在本地做了一個測試,在所有邏輯都相同的情況下,修改了載入模板的邏輯把:
@foreach ($topics as $topic)
@include('topics._topic_simple', ['topic' => $topic])
@endforeach
修改為:
@include('topics._topics')
然後在 topics._topics
裡:
@foreach ($topics as $topic)
<div class="py-2 simple-topic">
<div class="image-44 user-avatar">
...
</div>
</div>
@endforeach
由無數個 include
變成一個 include
,測試結果: