Vivacity InfoTech
Vivacity InfoTech

Reputation: 465

Add template to widgets un magento

I am integrate custom template to widget but its doesn't show the template data. below is my code

community/Sample/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>

community/Sample/Productslider/Block/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();
}
}

fronted/base/default/template/productslider/best-seller.phtml

echo $html;

It show blank page in front when i include widget in cms page. Any one can help me to find out the issue in my code. Thanks

Upvotes: 1

Views: 840

Answers (3)

Nicolas
Nicolas

Reputation: 11

To me your issue consists in extending Mage_Core_Block_Abstract in which _toHtml method return a void string.

Just set your template in your constructor and extends Mage_Core_Block_Template instead of Mage_Core_Block_Abstract

Upvotes: 1

St0iK
St0iK

Reputation: 638

You'll have to set the template in your Block Class

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

Create a folder under app/design/frontend/your_theme/default/my_module and add your html file

Upvotes: 0

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39089

You can assign a template to a block via the constructor (example taken from core file : /app/code/core/Mage/Catalog/Block/Layer/State.php:43)

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

So you would have to do something like this in your __contruct

$this->setTemplate('productslider/best-seller.phtml');

But what you seems to have trouble with is passing data to this template ?

Then, make is as you would in any other template : In your block :

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

In the template :

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

Upvotes: 0

Related Questions