How to Use Template File for Grid Column Renderer in Magento 1.9

gridmagento-1.9renderer

If I have a method in my renderer class:

public function render(Varien_Object $row)
{
    $rowValue = $row->getData($this->getColumn()->getIndex());
    $data = unserialize($rowValue);

    $result = '';

    if (is_array($data) && count($data) > 0) {
        foreach ($data as $orderItemData) {
            $result .= $orderItemData['name'] . ' </br>';
        }
    }
    return $result;
}

How can I render a template file here and return it?

Best Answer

A basic example to point you in the right direction:

public function render(Varien_Object $row)
{
    $block = $this->getLayout()->createBlock('core/template');
    $block->setTemplate('path/to/template.phtml');
    return $block->toHtml();
}

The render function is returning the final html for the column in question so any string you return from this function is valid. And if you need to pass your column object to the newly created block, just add $block->setColumn($this) before calling the ->toHtml() method and the column object will be available to you in your new block.

Related Topic