Magento objects in *.phtml files - magento

1) What $this/$resource variables refer to in Magento .phtml files?
2) Where do I find the list of methods I can use with $this/$resource?
print_r displays huge objects which functionality is not very easy to understand.

The template files (.phtml) are included in the block methods so a proper html can be returned. See the method Mage_Core_Block_Template::fetchView.
All of the block classes that have a template associated extend the Mage_Core_Block_Template class.
So $this is actually the current block class that is being used.
In theory one template can be used by multiple block classes. But this rarely happens.
If you don't know what $this is just add this in the template file:
echo get_class($this);
and you will get the class name.
If you want the available methods you can do this:
echo "<pre>"; print_r(get_class_methods($this));echo "</pre>";
But ...you will see that you are able to call some methods that don't really exist.
Because the class Mage_Core_Block_Template extends Varien_Object that implements the method __call you will be able to call any method that starts with get, set, uns, has without getting an error even if the method does not exist.
I haven't seen a phtml with the variable $resource but you can treat that the same as $this.

I didn't understand $resource that you are referencing with. But however $this stands for block that defines that template.
To make it more clear, suppose you have a layout code that seems like this
<some_handle>
<reference name="content">
<block type="xxx/yyy" name="custom.block" as="custom.block" template="custom/template.phtml" />
</reference>
</some_handle>
Now above in our demo layout, you can see a block is defined, which defines a template template.phtml. Now your template.phtml may look like this.
location : app/design/frontend/<package>/<theme>/template/custom/template.phtml
<div>
<?php $value = $this->getSomeMethod(); ?>
</div>
Here $this stands for the block that holds this template. In this case, $this is an instance of Namespace_Modulename_Block_Yyy block. (assume xxx stand for Namespace_Modulename module.).
So the method getSomeMethod() is not defined in this class, it will definitely shows an error.
Hope that gives you an idea.

Related

Magento - Block is not rendered

I'm developing my second Magento module, which should retrieve some data and render a block containing it. Such block would then be used by other pages.
At the moment, I have almost the whole module in place, but, for some reason, the block is not rendered when I call the controller method. I put some debug information, and I could see that the block's __construct() method is called correctly, but then the template doesn't seem to be loaded and the returned page is blank.
Here's the code for the block, which I copied from another module and modified:
class Company_CustomerData_Block_CustomerSummary extends Mage_Core_Block_Template {
const _TEMPLATE = 'customerdata/customersummary.phtml';
public function __construct() {
// This method is called correctly
parent::_construct();
$this->setTemplate(self::_TEMPLATE);
}
}
The file customersummary.phtml is in app/design/frontend/base/default/template/customerdata, which should be the correct place (or, at least, I think it is). Its content is the following:
It works!
Just some plain text. No tags, no code, nothing. I don't mind that it's a static text, it will be populated with data once complete.
In case of need, here's the code for the Controller (I removed the parts where the data is retrieved, as they don't make a difference):
public function dashboardAction() {
// Customer Data to render in the block
$CustomerData = array(); // Data is retrieved elsewhere
$this->getResponse()->setBody(
$this->getLayout()->createBlock('customerdata/customersummary')
->toHtml()
);
}
What could I be doing wrong? I'm afraid I made some stupid mistake again, but I really can't see it.
Finally two more questions:
How do I pass to the template the data I retrieve in the Controller? Specifically, variable $CustomerData.
Once the block renders, how do I render its content from within a page? The block should return a with some stuff in it, I'd like to render it inside the Customer Dashboard, just below the information that is already there.
Thanks in advance for the help.
Most classes on Magneto derive from Varien_Object. Varien_Object (and it's descendents) claim PHP's constructor (__construct) for themselves, and provide an _construct callback which you can use for whatever you like. What this means is that if you override the native PHP constructor (__construct) you need to remember to do a few things:
Accept the same number of parameters as the base class, and...
Call the parent constructor (parent::__construct) with the parameters your constructor was supplied.
Or, alternatively, use the _construct callback supplied by Varien_Object and you're done. There's no need to remember to call parent::_construct if you're using the Magento callback.
So to fix your code snippet above, you can either change...
public function __construct() {
... to ...
public function _construct() {
This will switch you over the using the Magento callback. Or you can change...
parent::_construct();
.. to ...
parent::__construct();
... to call the parent class' constructor. Remembering to add in the parameters that the parent class supplies.
Alan Storm write a great article about Magento's Block Lifecycle and call back methods recently, which might be of assistance.
To your other questions:
Passing information from the controller to a block is generally done via Magento's registry.
Rendering your template as part of a page requires that you create layout instructions (written in XML) which load your block and place it inside a parent block.
Although Jim's Answer is correct, I'm adding a second part because I found out what else was wrong: symbolic links. As I usually do when I develop plugins for a framework, I use symlinks to avoid copying the files over and over again. However, Magento uses function RealPath(), which resolves the symlink to its full path. As a result, the Template file to be loaded resides in a path outside Magento installation directory, and it can't be loaded for security reasons. This makes perfectly sense, pity it's just not very visible.
To fix the issue, I enabled Allow Symbolic Links in configuration on my Development PC, and now the Template is loaded and rendered correctly.
I think that Magento could do with a Log Viewer in the Admin interface. If there isn't a module that does it already, perhaps I should create one myself.
Thanks everybody for the help.

Magento - Review helper

I'm trying to instantiate the review helper class but failing..
I need to access getReviewsSummaryHtml() of class Mage_Review_Block_Product_View (app/code/code/Mage/Review/Block/Product/View.php).
I've tried this:
Mage::helper('review/product')->getReviewsSummaryHtml($_product, false, true);
But I receive Fatal error: Class 'Mage_Review_Helper_Product' not found.
What am I doing wrong?
(p.s. I can't use $this->getReviewsSummaryHtml as $this is out of scope.)
Thanks
The method getReviewsSummaryHtml() is defined in Mage_Review_Block_Product_View. You can instantiate it anywhere using Mage::app()->getLayout()->createBlock('review/product_view',$product);. In order for this to work, though, you'll need to also have a block instance named product_review_list.count, which is normally defined in review.xml, is of type core/template, and uses the review/product/view/count.phtml template.
You should simply do :
Mage::helper('review')
in order to get helper class which is named as Data.php
The function getReviewsSummaryHtml() resides in a block and you should only be able to call that function (ideally) from you template.
If you have moved that function to helper than you can call it like :
Mage::helper('review')->getReviewsSummaryHtml();
and you should do all these changes in your local.

Magento block type

In magento blocks what does block type means as in the below block?
<block type="core/template" template="example/view.phtml" />
Please help me in learning magento module development.
Is there some good resource?
The block "type" is a PHP Class. In a normal system, a
core/template
block type will become a Mage_Core_Block_Template class.
As for resources, start here (self link)
Here's what I learnt -
type refers to the PHP file that will provide the methods for this phtml. Examaple:
/app/code/local/modstorm/Block/Filename.php , where all your methods for this block resides. So if, for example, you are have something like $this->getMethodName() in your .phtml, you must have getMethodName() function declared in /app/code/local/modstorm/block/Filename.php.
Hope this helps others as well.

how can I get a reference to current Mage_Core_Controller_Varien_Action or decendant

I need a reference to the "current" running action from the code of a block.
i.e. I have a $this which is a descendant of Mage_Core_Block_Template and I need a way to call methods on the an instance of Mage_Core_Controller_Front_Action for the current request.
I hope this was clear enough.
Couple of things come to mind:
Mage::app()->getFrontController()
Also every block since all of them extends the Mage_Core_Block_Abstract have the following method:
$this->getRequest()
This return a Mage_Core_Controller_Request_Http which contains various methods like getControllerModule() or getModuleName().
HTH
Found out that I can use $this->getAction()

Codeigniter: Get Instance

What is the purpose of "Get Instance" in Codeigniter?
How would you explain this to a total beginner?
Ok, so everything in CodeIgniter runs through the super-magic $this variable. This only works for classes, as $this basically defines the current class.
Your controller is a class, so $this is there, allowing you to do $this->load->model('whatever');
In models, you are also using a class. It is slightly different here, as $this only contains useful stuff as you are extending from Model. Still, $this is still valid.
When you are using a helper or a library, you need to find that "instance" or $this equivalent.
$ci =& get_instance();
…makes $ci contain the exact same stuff/code/usefulness as $this, even though you are not in a class, or not in a class that inherits it.
That's an explanation for total beginners after 2 pints, so it's either wrong or about right. ;-)
It's an implementation of the singleton pattern. Essentially, there is only one instance of the class in question, which is designed to be accessible globally. The get_instance method is static and so provides a way of accessing the instance from anywhere in your code.

Resources