/macros/subdepartments.html保存完整的URL路徑
{% macro displayChildren(departments, parent_id, deep, prefix) %}
{% import _self as macros %}
{% for dept in departments %}
{% if dept.parent_id == parent_id %}
{% set href = dept.seo_title ~ '/' ~ prefix %}
<li><a href="/{{ href }}/{{ dept.id }}/dept" class="black" style="margin-left: {{ deep * 10 }}px;">{{ dept.title }}</a></li>
{{ macros.displayChildren(departments, dept.id, deep + 1, href) }}
{% endif %}
{% endfor %}
{% endmacro %}
common.html
<div class="departments">
<ul class="nav">
<div class="clearfix">
{% import '/macros/subdepartments.html' as macros %}
{% for dept in common.departments %}
{% if dept.parent_id == 0 %}
<li><a href="/{{ dept.seo_title }}/{{ dept.id }}/dept" class="black">{{ dept.title }}</a><div class="dropdown-menu"><ol class="nav">{{ macros.displayChildren(common.departments, dept.id, 0, dept.seo_title) }}</ol></div></li>
{% endif %}
{% endfor %}
</div>
</ul>
</div>
department.html(由Twig的common.html擴展)
<ol class="nav" style="margin-left: 10px;">
{% for dept in common.departments %}
{% if dept.id == selected_dept.parent_id %}
<li><a href="/{{ dept.seo_title }}/{{ dept.id }}/dept" class="black">← {{ dept.title }}</a></li>
{% endif %}
{% if dept.parent_id != 0 or dept.id == selected_dept.id %}
{% if dept.parent_id == selected_dept.parent_id %}
<li><a href="/{{ dept.seo_title }}/{{ dept.id }}/dept" class="black{% if dept.id == selected_dept.id %} selected{% endif %}">{{ dept.title }}</a></li>
{{ macros.displayChildren(common.departments, dept.id, 1) }}
{% endif %}
{% endif %}
{% endfor %}</ol>
common.departments陣列
Array ([0] => Array ([id] => 1 [parent_id] => 0 [title] => Моторные масла [seo_title] => motor-oils [full_title] => [display] => 1) [1] => Array ([id] => 2 [parent_id] => 0 [title] => Трансмиссионные масла [seo_title] => transmission-oils [full_title] => [display] => 1) [2] => Array ([id] => 3 [parent_id] => 0 [title] => Присадки [seo_title] => additives [full_title] => [display] => 1) [3] => Array ([id] => 4 [parent_id] => 0 [title] => Автокосметика [seo_title] => autocosmetics [full_title] => [display] => 1) [4] => Array ([id] => 5 [parent_id] => 0 [title] => Сервисные продукты [seo_title] => service-products [full_title] => [display] => 1) [5] => Array ([id] => 6 [parent_id] => 0 [title] => Технологические жидкости [seo_title] => process-fluids [full_title] => [display] => 1) [6] => Array ([id] => 7 [parent_id] => 0 [title] => Зимняя программа [seo_title] => winter-program [full_title] => [display] => 1) [7] => Array ([id] => 8 [parent_id] => 1 [title] => Синтетические [seo_title] => synthetic [full_title] => Синтетические моторные масла [display] => 1) [8] => Array ([id] => 9 [parent_id] => 1 [title] => Полусинтетические [seo_title] => semisynthetic [full_title] => Полусинтетические моторные масла [display] => 1) [9] => Array ([id] => 10 [parent_id] => 1 [title] => Минеральные [seo_title] => mineral [full_title] => Минеральные моторные масла [display] => 1) [10] => Array ([id] => 11 [parent_id] => 2 [title] => для МКПП (механика) [seo_title] => mt [full_title] => Трансмиссионные масла для МКПП [display] => 1) [11] => Array ([id] => 12 [parent_id] => 2 [title] => для АКПП (автомат) [seo_title] => at [full_title] => Трансмиссионные масла для АКПП [display] => 1) [12] => Array ([id] => 13 [parent_id] => 12 [title] => Test [seo_title] => test [full_title] => Test [display] => 1))
在common.html一切ok。在deparment.html中,我需要在{{dept.seo_title}}中獲得相同的href。如果我從department.html中的common.html去/在/傳輸-leaf/13/dept部門,我會看到錯誤的URL。
← Трансмиссионные масла (href: /transmission-oils/2/dept right!)
для МКПП (механика) (href: mt/11/dept wrong!, need to be /mt/transmission-oils/11/dept)
для АКПП (автомат) (href: at/12/dept wrong!, need to be /at/transmission-oils/12/dept)
Test (href: /test//13/dept wrong!, need to be /test/at/transmission-oils/13/dept)
那麼,如何更改department.html中的代碼以打印完整路徑到子系統?也許遞歸函數會得到所有的父部門。
你可以請print_r'common.departments'這樣我們得到的上下文? –
是的,我更新了我的文章 – Tyler21
謝謝。你想在哪裏保存完整的鏈接? –