我试图循环播放的菜单如下所示:
"menu" => array(
array(
"title" => "Nieuws",
"url" => "#"
),
array(
"title" => "Standen",
"items" => array(
array(
"title" => "Eredivisie",
"url" => "#"
),
array(
"title" => "Topscorers",
"url" => "#"
),
array(
"title" => "Assists",
"url" => "#"
),
array(
"title" => "Kaarten",
"url" => "#"
),
)
),
array(
"title" => "Kalender",
"items" => array(
array(
"title" => "Eredivisie1",
"url" => "#"
),
array(
"title" => "Topscorers1",
"url" => "#"
),
array(
"title" => "Assists1",
"url" => "#"
),
array(
"title" => "Kaarten1",
"url" => "#"
),
)
),
array(
"title" => "PSV",
"url" => "#"
),
array(
"title" => "PSV Watchers",
"url" => "#"
),
array(
"title" => "Transfers",
"url" => "#"
)
)
)
如果数组中的menuitem,则表示它将显示为下拉菜单。
目前,我使用此代码:
{% for menuitem in site.menu %}
{% if menuitem.items is defined %}
<li class="c-drop">
<a href="#">{{ menuitem.title }}</a>
<div class="c-drop-menu">
<ul class="list-inline">
{% for items in menuitem %}
{% endfor %}
</ul>
</div>
</li>
{% else %}
<li><a href="{{ menuitem.url }}">{{ menuitem.title }}</a></li>
{% endif %}
{% endfor %}
如果menuitem
是array
,则会显示Array
,其错误类似于&#34;数组到字符串转换&#34;。
我怎样才能使这个工作,所以我得到菜单项,如果它是一个数组,循环它,但仍然得到菜单项?
我是否需要更改阵列才能使其正常工作?
预期的菜单结果:
更新
当前代码:
{% for menuitem in site.menu %}
{% if menuitem.items is defined %}
<li class="c-drop">
<a href="#">{{ menuitem.title }}</a>
<div class="c-drop-menu">
<ul class="list-inline">
{% for items in menuitem %}
<li><a href="{{ items.url }}">{{ items.title }}</a></li>
{% endfor %}
</ul>
</div>
</li>
{% else %}
<li><a href="{{ menuitem.url }}">{{ menuitem.title }}</a></li>
{% endif %}
{% endfor %}
答案 0 :(得分:1)
您正在尝试在href中呈现值,如果值是数组,则会将数组抛出到字符串转换。 loop through the keys更好一点,如果密钥的值是可迭代的,那么渲染密钥
{% for menuitem in site.menu|keys %}
{% if site.menu[menuitem] is iterable %}
<li class="c-drop">
<a href="#">{{ menuitem }}</a>
<div class="c-drop-menu">
<ul class="list-inline">
{% for item in site.menu[menuitem] %}
<li><a href="#">{{ item }}</a></li>
{% endfor %}
</ul>
</div>
</li>
{% else %}
<li>
<a href="#">{{ site.menu[loop.index0] }}</a>
</li>
{% endif %}
{% endfor %}
答案 1 :(得分:1)
好的,我使用了这样的is defined
:
{% for menuitem in site.menu %}
{% if menuitem.items is defined %}
<li class="c-drop">
<a href="#">{{ menuitem.title }}</a>
<div class="c-drop-menu">
<ul class="list-inline">
{% for items in menuitem.items %}
<li><a href="{{ items.url }}">{{ items.title }}</a></li>
{% endfor %}
</ul>
</div>
</li>
{% else %}
<li><a href="{{ menuitem.url }}">{{ menuitem.title }}</a></li>
{% endif %}
{% endfor %}
感谢@Matt Smeets的帮助