[英]How to load a Custom PHP Magento Block inside a template file
我根據本教程創建了一個自定義塊http://blog.magikcommerce.com/how-to-show-most-viewed-best- sell-products-in-magento-store
我想從我的 home.phtml 模板文件中調用 Block。
我從以下位置調用我的靜態塊:
<?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
從模板文件加載塊是一種非常糟糕的風格,但它是可能的。
來自模板文件的骯臟方式
echo $this->getLayout()->createBlock('catalog/product_bestseller')->toHtml();
干凈的方法:
修改對應的布局XML文件並添加塊,然后用
echo $this->getChildHtml('product_bestseller');
如果要將塊添加到 cms 頁面,請使用Design下的Layout Xml Updates部分:
<reference name="content">
<block type="catalog/product_bestseller" name="product_bestseller" />
</reference>
這從 1.5.1 開始工作,還允許您重新定位模板
$block = $this->getLayout()
->createBlock('catalog/product_bestseller','product_bestseller',
array('template' => 'pathTo/template.phtml'));
echo $block->setBlockId('whatever')->toHtml();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.