Calling Models in Magento Templates - magento

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.

Related

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

magento - include core functionality in custom module

I've recently made my first couple of simple modules for magento. Basically just trying out making a module that printed 'hi'. Then one that included the site design, and finally one that inserted it's own block into the content area to print some stuff.
Now, what I really want with this module is to be able to list products (using the same layout as normal product listing) but based on ID's. Like from cookies or as an example, static array of product ID's.
How would I go about to reuse the product listing code as much as possible and retaining the theming for it? Also if there is some small detail I wanted to change in the layout - how could i make a small template adjustment on the fly/programmatically?
Is this even the right way to go about it, to create a new module and somehow include existing functionality in it? Or should I just try to recreate the product listing and mimic the design?
Lots of questions I know, but it's hard to find relevant information beyond the very simplest modules. TIA.

Magento - is a block always needed for a widget?

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).

Magento get which layout being used on phtml files

Is there a way I could get which layout being used on a certain phtml files?
Here in my case, I want to check what layout being used on catalog/list.phtml, I used that information to make conditional "if" on the product image grid size.
I've tried to google it out. But all the result is just explaining about xml layout things. The closest clue I got is this thread
Magento get layout for given page
which stated the use of this snippet
$left_block = $this->loadLayout()->getLayout()->getBlock('left');
but when I tried it on the phtml files, I got an exception error
UPDATE
joe's answer below has give me some more clue, the exception gone. But the behavior doesn't really what I need. That snippet of code seems to be just check whether the specified block is defined on the XML. What I really need is whether that block exist on a certain page.
In my case, I need to check what layout being used on catalog/product/list.phtml. if it's 3 columns, I'm gonna make the image resized smaller. If it 1 column, I'll make it bigger.
Is there any way I could do that??
If I read the question correctly, then try:
$this->getLayout()->getBlock('root')->getTemplate();
Remove loadLayout():
$left_block = $this->getLayout()->getBlock('left');
By the time you are in the PHTML file, the layout is already loaded.
In PHTML files, $this refers to the Mage_Core_Block_Template class (or a class that extends it). This class doesn't have a loadLayout() method defined, which is why you get an exception; instead, loadLayout() is part of Mage_Core_Controller_Varien_Action.

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