You are here: Home » Magento » Dynamically add widget to .phtml file in Magento template
Dynamically add widget to .phtml file in Magento template
Written by sander on Aug 02, 2010 | 1 Comments
Recently I downloaded a Magento module that seemed to do what I want: a list of tweets of a certain user, displayed in the frontend of Magento.
However, the module was a widget. I did not want to create a CMS page or block solely for inputting the widget’s variable so I sorted out how to call the widget from inside a .phtml file in a Magento template.
The widget was Magentweet and had several things to configure when using it in the backend. I could add the same widget programmatically like this:
<?php $tweetblock = $this->getLayout()->createBlock(‘magentweet/user’);
$tuser = $tweetblock->setData(‘tuser’,'groupdeal_nl’);
$nb = $tweetblock->setData(‘nb’, 5);
$link = $tweetblock->setData(‘link’,'#1E7EC8′);
$link_a = $tweetblock->setData(‘link_a’,'#000′);
$desa = $tweetblock->setData(‘desa’,'enable’);
echo $tweetblock->setTemplate(‘magentweet/user.phtml’)->toHtml(); ?>
Of importance here is not that a widget block is created through a regular createBlock() statement. The trick is the setData() statements. In a widgets Block code you can easily see which data the widget expects, and you can set the widget’s data accordingly from the template instead of through elaborate WYSIWYG cms procedures.
The Author of this post is sander
Versatile web enthusiast. Specialized in e-commerce and the technology behind that.
Popular Posts
-
Fix for Magento 1.4.0.1 PayPal and shipping costs
-
Ultimate Magento SEO tips and tricks – part 1: content
-
10 (Magento) front-end development best practices
-
Case study: How Magento’s flexibility allowed us to quickly sell a completely different product with relative ease
-
Tailoring Magento to B2B e-commerce
Become a member for FREE!
Categories
- Blog (12)
- Conversion & pursuasion (6)
- Ecommerce (20)
- Ecommerce Basics (4)
- Extensions (6)
- Featured (1)
- Magento (45)
- Security (1)
- SEO (3)
- Webdesign (1)
E-commerce Archives
- April 2012 (3)
- March 2012 (3)
- February 2012 (2)
- January 2012 (2)
- December 2011 (4)
- November 2011 (4)
- October 2011 (3)
- September 2011 (3)
- July 2011 (1)
- June 2011 (4)
- May 2011 (7)
- March 2011 (1)
- February 2011 (2)
- January 2011 (2)
- December 2010 (5)
- September 2010 (1)
- August 2010 (1)
- July 2010 (1)
- October 2009 (1)
- September 2009 (3)
- August 2009 (2)
- July 2009 (9)
- June 2009 (6)

1 Responses »
Trackbacks