/macros/subdepartments.html
{% 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一切都好。在deparment.html中,我需要在{{dept.seo_title}}中获得相同的href。如果我从department.html的common.html转到/tir/ oils / oils / 13 / dept,我会看到错误的部门网址。
← Трансмиссионные масла (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中的代码以打印子部门的完整路径?也许递归函数将获得所有父部门。
答案 0 :(得分:0)
据我了解,你错过了hrefs中的父路径。
我认为你说的时候会出现问题:
Subdept1_2_1 (link: subdept1_2_1-subdept1_2-subdept1-dept1)
难道你不是指上升顺序的树,例如:
Subdept1_2_1 (link: dept1-subdept1-subdept1_2-subdept1_2_1)
无论如何,你可以试试这样的代码:
main.twig
{% import 'macros.twig' as macros %}
{% for dept in 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(departments, dept.id, 0, dept.title) }}</ol>
</div>
</li>
{% endif %}
{% endfor %}
macros.twig
{% macro displayChildren(departments, parent_id, deep, prefix) %}
{% for dept in departments %}
{% if dept.parent_id == parent_id %}
{% set href = prefix ~ '-' ~ dept.seo_title %}
<li><a href="/{{ href }}/{{ dept.id }}/dept" class="black" style="margin-left: {{ deep * 10 }}px;">{{ dept.title }}</a></li>
{{ _self.displayChildren(departments, dept.id, deep + 1, href) }}
{% endif %}
{% endfor %}
{% endmacro %}
注意:要使用节点的反向路径,请使用:
{% set href = dept.seo_title ~ '-' ~ prefix %}
而不是:
{% set href = prefix ~ '-' ~ dept.seo_title %}
注意:我更改了文件名以获得更好的重现性。