Marlon Creative
Marlon Creative

Reputation: 3846

Magento - adding a widget instance to a template file

I know that you can call a cms block directly from a template file using the following:

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('footer_links')->toHtml() ?>

Is there some way to do this with widget instances?

Upvotes: 2

Views: 19870

Answers (4)

Japhet Perez Atristain
Japhet Perez Atristain

Reputation: 326

In your template:

<?php
$filter = Mage::getModel('widget/template_filter');
echo $filter->filter('{{widget type="cms/widget_page_link" template="cms/widget/link/link_block.phtml" page_id="2"}}');
?>

Upvotes: 21

Sonia Simina
Sonia Simina

Reputation: 51

Andrew missed toHtml() function at the end:

<?php echo $this->getLayout()->createBlock('cms/widget_page_link')->setTemplate('cms/widget/link/link_block.phtml')->setPageId(2)->toHtml(); ?>

Upvotes: 5

Andrew
Andrew

Reputation: 21

The answer above may work, but the same thing can be achieved by loading the widget as you would a static block and passing in the custom parameters using the magic setters like this:

<?php echo $this->getLayout()->createBlock('cms/widget_page_link')->setTemplate('cms/widget/link/link_block.phtml')->setPageId(2); ?>

Upvotes: 2

Jorge Herv&#225;s
Jorge Herv&#225;s

Reputation: 21

To know the correct parameters "type", "template" and more, you can use the "Insert widget" button on the graphical editor in a block/page template, then you click the show/hide editor and you get the code

Upvotes: 2

Related Questions