Magento – Can you load a widget instance from a template

blockstemplatewidgets

I'd like to create 'widgets' in certain areas on my site, that can have certain parameters configurable by a non-technical Magento store admin. Widgets, by all descriptions sound like a nice way of doing this. All the adminhtml elements are in place to allow them to fill in the parameters without too much hassle.

What I can't work out though, is how to load and render a specific widget instance at a particular point in a template (as created via CMS > Widget > Add New Widget Instance and stored in widget_instance). I used the layout update feature to insert it into a block, which works reasonably well, but it's not quite flexible enough for what I wish to achieve.

Is it possible in a template file to load and render a widget instance?

Best Answer

Yes, you can do something like this:

$widget = Mage::getModel('widget/widget_instance')->load($widgetId);
$widgetBlock = $this
                   ->getLayout()
                   ->createBlock($widget->getType(),
                                 $widget->getTitle(),
                                 $widget->getWidgetParameters());
echo $widgetBlock->toHtml();