我假设你已经习惯了 nested-tree [ https://octobercms.com/docs/database/traits#nested-tree ]。
nested-tree
首先我们只是传递父母 parentCats 到我们的视图/页面
parentCats
use HardikSatasiya\SoTest\Models\Categories; function onStart() { $this['parentCats'] = Categories::getAllRoot(); }
在页面/视图中显示 category 和它的 sub-categories 取决于 N level 我们可以用这个 render_cats macro
category
sub-categories
N level
render_cats macro
{% import _self as thisPage %} {% macro render_cats(items) %} {% import _self as thisPage %} {% for item in items %} <li>{{ item.level }} {{ item.title }} {% if item.childcount > 0 %} <ul> {{ thisPage.render_cats(item.children) }} </ul> {% endif %} </li> {% endfor %} {% endmacro %} <ul> {{ thisPage.render_cats(parentCats) }} <!-- we are passing parent cats ^ here --> </ul>
产量
<ul> <li> 0 Parent Cat 1 <!-- ^ this is level --> <ul> <li> 1 Sub cat 1 <ul> <li>2 Sub Sub cat 1 </li> <li>2 Sub Sub cat 3 </li> <li>2 Sub Sub cat 3 </li> </ul> </li> <li>1 Sub cat 2 </li> </ul> </li> <li> 0 Parent Cat 2 <ul> <li>1 Sub cat 3 </li> <li>1 Sub cat 4 </li> </ul> </li> </ul>
如有疑问请发表评论