Magento - is a block always needed for a widget? - magento

I just started to develop Magento widgets and thereby I came to this problem/question:
It is possible to tell a Magento widget not to use a block-class and directly render a template file?
Thanks a lot

Block is needed. If you don't want to create a new block, just give block type as cms/block. Remember, in your phtml template files, then you will not have your custom functions call by using $this (as $this references to Block).

Related

Mage::app()->getLayout()->getBlock('content'); returns false Magento 1.9

I am trying to add a block dynamically after the content, the block should load in every category and every product page and base on the category title or page title load some data from database and show.
I use controller_action_layout_load_before, and inside the method in observer class,
Mage::app()->getLayout()->getBlock('content');
returns false instead of an object.
I have to implement it for a client as a Magento module, I have no access to their template, I am using Magento 1.9 rwd/default
I think that using controller_action_layout_load_before is not good; it is too early because the XML layout is not built yet.
Try to use event controller_action_layout_generate_xml_before.
Or maybe adding your own XML layout could be better solution. See http://alanstorm.com/adding-additional-layout-xml-updates-via-modules/

Magento widget rendering in non-CMS content

Is there a simple way to hook into the Magento widget rendering feature on something other than CMS block or page?
I have a tooltips extension to display tooltips on custom options. There will be lots of products all with the same options and all needing the same tooltip text. While the tooltip extension we're using provides a means to declare snippets that can then be used on products, the snippets are added to individual products at setup time rather than referencing the single snippet instance. Thus if the tooltip content needs to change across all products we have to edit the snippet and then reapply the modified snippet to all products.
It would be preferable to be able refernce the snippet direct rather than just using it at product setup, but that's not how it works. So an alternative would be to include a static block in the tooltip description and reference the single description instance with our theme's already provided widget feature that works for on CMS pages e.g. {{widget type="cms/widget_block" template="cms/widget/static_block/default.phtml" block_id="xx"}}, where xx is the block created for this tooltip.
This needs the tooltip HTML description parsed through whatever it is in Magento that parses content HTML and processes any widget directives it contains.
I tried the following, where $tipstext is the tooltips HTML containing the widget directive, but no go. Didn't think it would be that simple!
Mage_Cms_Model_Template_Filter::filter($tipstext);
Anyhone have any idea if/how this could be achieved easily?
The class Mage_Cms_Model_Template_Filter does not have a widgetDirective method so it does not know how to parse {{widget}} short codes. Try instead Mage_Widget_Model_Template_Filter:
Mage::getSingleton('widget/template_filter')->filter($text);
This should work
<?php
$filter = new Mage_Widget_Model_Template_Filter();
$_widget = $filter->filter('{{widget type="cms/widget_page_link" template="cms/widget/link/link_block.phtml" page_id="2"}}');
echo $_widget;
?>

Calling Models in Magento Templates

I am currently working on integrating HTML cuts into Magento's template, however, I am just a little stumped on the structure of Magento itself. I want to list all of the categories inside a custom template in the 'navigation/left'.phtml file. The following accepted answer Magento: Display sub-category list seems to do what I need to do, however,I don't feel comfortable in calling a model inside of a view files as in MVC, which the accepted answer has done.
Is there a better way of putting this in another section of Magento, or perhaps a custom block which extends the Block_Catalog_Product_List class would be a better way of retrieving the categories?
Thanks
The simplest way to do it is to create a module with a helper inside it, that returns the data you need. Then in the template file call this:
$data = Mage::helper('myhelper')->getCategoryList();
//do your magic with $data
There is no point in overriding blocks unless there is no other solution.

Magento call block for specific product in list.phtml

I want to print some additional data for each product on list.phtml that will depend on the product. For example, print html with New and Sale labels for each product (this is just an example). I want to somehow separate logic from .phtml file and remain in phtml just a call for it. What is the best way to achieve it? Also I want to minimize overriding core files and make it more independent.
My ideas are:
Create my own block, create and call it in .phtml and pass there my
product with setData:
$this->getLayout()->createBlock("namespace/block")->
setTemplate("path/to/template")->setData('product', $_product)
and then call getProduct() inside my block .php file. But for some reasons that doesn't work.
The best-looking solution for me, but not sure if it's allowed. Create new block in layout files and call it with getChildHtml('block_name'). But I don't know how to pass there current product or how to make it to be able to access through $this inside my .php file.
Override product block and add my own methods like getRibbons(). The worst solution for me because this will requre html writing in .php block and will override core block.
I'm pretty new to magento, maybe I am missing some basic concepts?
Extend your Product Block PHP and add new methods you want in it. Then in your module.xml where you are referencing core Product Block, replace it with your newly created Block.
Now, from PHTML files you can easily call the PHP block's methods using $this

how to call functions/methods within CMS block or page?

We are trying to make all our blocks and pages static so that designer or anyone else can easily change the content or design of the website, however. There is a feature that uses our own custom module. So, the template that we want to make static is calling methods out of our custom block, for example,
<!--some html code-->
.....
<?php $this->helpMeBePartOfCMS(); ?>
.....
<!--some html code-->
How do i incorporate these method calls inside cms block or page?
Thank you
The CMS system makes it easy to include custom blocks (or widgets in Enterprise), but not so easy to make method calls. This is because the templates are never parsed as PHP, so you cannot just include a PHP tag.
Why don't you define a custom block to accomplish this? If that won't work, please provide a little more detail about what you're actually trying to include, so that we can troubleshoot further.
Hope that helps!
Thanks,
Joe
Since CMS block templates are not parsed as PHP code, there's no way to inject a call as you are describing. However, all classes descending from Mage_Core_Block_Abstract have the ability to call child and parent blocks. Use one of these methods to do what you are looking for from the block directly:
getParentBlock
getChild
getSortedChildren
there is a way that you call these php codes in any phtml file (create a new and save any where in your templates) then call that phtml file in your cms page as a block (which we usually do for calling blocks)

Resources