2013-09-24 172 views
6

裏面我已經根據本教程 http://blog.magikcommerce.com/how-to-show-most-viewed-best-selling-products-in-magento-store如何加載自定義PHP Magento的模塊模板文件

我想從我的home.phtml模板文件調用塊中創建一個自定義模塊。

我打電話給我的靜態阻止:

<?php 
$helper = Mage::helper('cms'); 
$source = Mage::getModel('cms/block')->load('my-block'); 
$processor = $helper->getPageTemplateProcessor(); 
$html = $processor->filter($source->getContent()); 
echo $html; 
?> 

它就像一個魅力,當然! ' 但我怎麼能加載動態塊,在我的情況下,在模板文件內。

我bestseller.phtml文件是:

app/design/frontend/default/default/template/catalog/product/bestseller.phtml 

我的類是:從一個模板文件

Mage_Catalog_Block_Product_Bestseller 

回答

17

加載模塊是一個非常糟糕的風格,但它是可能的。

從模板文件

echo $this->getLayout()->createBlock('catalog/product_bestseller')->toHtml(); 

髒方式的清潔方式:
去你的佈局XML文件中添加塊像任何其它並參考其與

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

如果您在cms頁面中使用設計下的「Layout Xml Updates」部分,如

<reference name="content"> 
    <block type="catalog/product_bestseller" name="product_bestseller" /> 
</reference> 
+0

完美!這個解決方案就像一個魅力! –

2

這個工作爲1.5.1,也可以讓你重新定位模板

$block = $this->getLayout() 
     ->createBlock('catalog/product_bestseller','product_bestseller', 
         array('template' => 'pathTo/template.phtml')); 
echo $block->setBlockId('whatever')->toHtml();