2015-01-15 98 views
-3

/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中的代碼以打印完整路徑到子系統?也許遞歸函數會得到所有的父部門。

+0

你可以請print_r'common.departments'這樣我們得到的上下文? –

+0

是的,我更新了我的文章 – Tyler21

+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 %} 

注:我改變文件名稱更好的重現性。

+0

我也改變了文件名。 common.html(main.twig)中的代碼效果很好。在departmens.html中有一個href問題(其他東西也可以) – Tyler21

+0

給定的代碼爲您提供了完整的一代菜單(帶有完整的hrefs)。我不明白你想要什麼。 –

+0

請看看aliexpress.com。您將看到帶下拉子菜單的菜單。當你去任何子類別,你會看到左側塊的子類別地圖。例如:電子產品 - >平板電腦。在左側:相關分類 <電腦及辦公室 平板電腦。在類別菜單中,我有完整的類別鏈接,但是如果我轉到子類別(子部門),我無法獲得完整的seo_title,因爲腳本僅顯示直接父級,而不是所有父級。所以,如果我去到第5級子類別,父級將子類別4,但我需要另一位家長來完成href – Tyler21