将模板添加到magento中的小部件

时间:2015-01-15 06:09:40

标签: magento

我将自定义模板集成到窗口小部件,但它不显示模板数据。下面是我的代码

社区/样品/ Productslider的/ etc / widget.xml

<widgets>
<productslider_bestseller type="productslider/bestseller" translate="product slider option" module="productslider">
    <name>Best Seller Product</name>
    <description type="desc">Enable the Best Seller Product Widget</description>
<parameters>
  <template>
            <required>1</required>
            <visible>1</visible>
            <label>Template</label>
            <type>select</type>
     <values>
       <best-seller translate="label">
                    <value>productslider/best-seller.phtml</value>
                    <label>Best Seller</label>
                </best-seller>
     </values>
     </template>
   </parameters>
   </productslider_bestseller>
</widgets>

社区/样品/ Productslider /砌块/ Bestseller.php

class Sample_Productslider_Block_Bestseller extends Mage_Core_Block_Abstract implements Mage_Widget_Block_Interface
{

protected function _construct()
{
    parent::_construct();
}
protected function _toHtml()
{
    $pageTitle = '';
    $headBlock = $this->getLayout()->getBlock('head');
    if ($headBlock) {
        $pageTitle = $headBlock->getTitle();
    }

    $html = "test";
    $this->assign('best-seller', $html);
    return parent::_toHtml();
}
}

额/碱/默认/模板/ productslider /最佳seller.phtml

echo $html;

当我在cms页面中包含widget时,它显示在前面的空白页面。 任何人都可以帮助我在我的代码中找出问题。 谢谢

3 个答案:

答案 0 :(得分:1)

对我来说,您的问题在于扩展Mage_Core_Block_Abstract,其中_toHtml方法返回空字符串。

只需在构造函数中设置模板并扩展Mage_Core_Block_Template而不是Mage_Core_Block_Abstract

答案 1 :(得分:0)

您可以通过构造函数将模板分配给块(示例取自核心文件:/app/code/core/Mage/Catalog/Block/Layer/State.php:43

public function __construct()
{
    parent::__construct();
    $this->setTemplate('catalog/layer/state.phtml');
}

所以你必须在你的 __ contruct

中做这样的事情
$this->setTemplate('productslider/best-seller.phtml');

但您似乎遇到的问题是将数据传递给此模板?

然后,make就像在任何其他模板中一样: 在您的区块中:

public function getSomeExtraHtml(){
    return 'test';    
}

在模板中:

<?php echo $this->getSomeExtraHtml() ?>

答案 2 :(得分:0)

您必须在Block Class中设置模板

protected function _toHtml()
{
   $this->setTemplate('my_module/widget.phtml');
   return parent::_toHtml();
}

在下面创建一个文件夹 app / design / frontend / your_theme / default / my_module并添加你的html文件