2012-11-01 129 views
0

我試圖在產品視圖(僅)產品詳細信息頁面中將模板塊添加到我的頁腳塊中。我試着在catalog.xml中佈局文件下面,但沒有運氣:爲模塊動態添加模板塊

<catalog_product_view translate="label"> 
    ... 
    <reference name="footer"> 
     <block type="core/template" name="uniqueName" template="catalog/product/mytemplate.phtml" /> 
    </reference> 
</catalog_product_view> 

<catalog_product_view translate="label"> 
    ... 
    <reference name="footer"> 
     <block type="core/template" name="uniqueName"> 
      <action method="setTemplate"><template>catalog/product/mytemplate.phtml</template></action> 
     </block> 
    </reference> 
</catalog_product_view> 

我能夠用後一種方法把模板塊成塊content同樣的方式,<reference name="content">,所以我不明白爲什麼這是行不通的。這似乎是我沒有正確引用頁腳..我看到在page.xml文件中創建的頁腳添加爲<block type="page/html_footer" name="footer" as="footer" template="page/html/footer.phtml">。有人可以幫我解決這個問題嗎?非常感激!

+0

你在哪裏回聲$這個 - 調用>,直到最底層(「uniqueName」)? – pspahn

+0

是的,我其實只是注意到這是我的問題。頁腳模板不調用'$ this-> getChildren();'.. –

+0

就像我假設的那樣>< –

回答

1

page.xml,看看該實例化content塊對象

<block type="core/text_list" name="content" as="content" translate="label"> 

佈局更新XML片段content區塊是core/text_list區塊。 core/text_list塊會自動呈現其子塊(即它們是文本塊列表的包含塊)。 core/text_list別名解析爲Mage_Core_Block_Text_List,看看這個類的渲染方法,看看爲什麼把東西附加到你的內容塊的作品。

現在,查看實例化頁腳塊的佈局更新XML片段。

<block type="page/html_footer" name="footer" as="footer" template="page/html/footer.phtml"> 

腳塊文本列表框。這是一個page/html_footer塊,它是一個模板塊。您可以通過查看該page/html_footer塊從

class Mage_Page_Block_Html_Footer extends Mage_Core_Block_Template 

模板塊繼承不自動呈現其所有子塊類確定這一點。相反,塊的模板中,你必須明確地渲染調用一個孩子

echo $this->getChildHtml('block_name'); 

所以,當你說

<reference name="footer"> 
    <block type="core/template" name="uniqueName" template="catalog/product/mytemplate.phtml" /> 
</reference> 

你告訴Magento的插入一個名爲uniqueName作爲一個孩子塊塊的footer塊。然而,爲了使塊,頁腳的模板必須調用

$this->getChildHtml('uniqueName') 
1

您需要確保該塊模板(在我的情況頁腳塊)你想添加塊呼喚您在佈局XML已經加上你的孩子塊..

footer.phtml :

<?php echo $this->getChildHtml('uniqueName'); ?>