[英]How to load a Custom PHP Magento Block inside a template file
I've created a Custom Block based on this tutorial http://blog.magikcommerce.com/how-to-show-most-viewed-best-selling-products-in-magento-store我根据本教程创建了一个自定义块http://blog.magikcommerce.com/how-to-show-most-viewed-best- sell-products-in-magento-store
I would like to call the Block from my home.phtml template file.我想从我的 home.phtml 模板文件中调用 Block。
I call my static blocks from:我从以下位置调用我的静态块:
<?php
$helper = Mage::helper('cms');
$source = Mage::getModel('cms/block')->load('my-block');
$processor = $helper->getPageTemplateProcessor();
$html = $processor->filter($source->getContent());
echo $html;
?>
And it works like a charm, of course!当然,它就像一种魅力! ' But how can I load dynamic blocks, in my case, inside template files. ' 但是,在我的情况下,如何在模板文件中加载动态块。
My bestseller.phtml file is:我的 bestseller.phtml 文件是:
app/design/frontend/default/default/template/catalog/product/bestseller.phtml
And my class is:我的班级是:
Mage_Catalog_Block_Product_Bestseller
Loading a block from a template file is a very bad style, but it is possible.从模板文件加载块是一种非常糟糕的风格,但它是可能的。
The dirty way from a template file来自模板文件的肮脏方式
echo $this->getLayout()->createBlock('catalog/product_bestseller')->toHtml();
The clean way:干净的方法:
Modify the corresponding layout XML file and add the block, then refer to it with修改对应的布局XML文件并添加块,然后用
echo $this->getChildHtml('product_bestseller');
If you want to add a block to a cms page use the Layout Xml Updates section under Design :如果要将块添加到 cms 页面,请使用Design下的Layout Xml Updates部分:
<reference name="content">
<block type="catalog/product_bestseller" name="product_bestseller" />
</reference>
this worked as of 1.5.1, also allows you to relocate the template这从 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.