0

I Have Doubt I want to use Same phtml file in another phtml but it won't work Because of $this

I want to know Why it's not working and How to use another phtml

  • $this refer to the current object and in magento's phtml file $this means block class of that phtml file. – Minesh Patel Mar 25 '16 at 6:26
1

Use $this to refer to the current object.

To use $this either you add the Method in your current block class or use the same block class.

| improve this answer | |
  • I am using this <?php if($this->getCollection()->getSize()): ?> how to find current method – Magento 2 Mar 25 '16 at 6:29
  • Can you please give me some example – Magento 2 Mar 25 '16 at 6:29
  • check for getCollection Method or check __construct Method – Qaisar Satti Mar 25 '16 at 6:41
  • here is example php.net/manual/en/ref.classobj.php – Qaisar Satti Mar 25 '16 at 7:46
3

Let me simply explain it. Hope you are familiar with Magento's rendering process.

Generally in Magento each template file is associated with a Block class. For instance assume you have below two blocks

class Mynamespace_Mymodule_Block_Apple extends Mage_Core_Bock_Template
{

   public function getMyCollection()
   {
        // Some code here
   }

}

______________________________________________________________________


class Mynamespace_Mymodule_Block_Orange extends Mage_Core_Bock_Template
{

   public function getMyCollection()
   {
        // Some code here
   }

}

Assume the class Mynamespace_Mymodule_Block_Apple associated with a template file named apple.phtml whereas the class Mynamespace_Mymodule_Block_Orange is associated with a template file called orange.phtml.

Then the keyword $this inside apple.phtml refers the class object of it's associated block. In our case its Mynamespace_Mymodule_Block_Apple

Similarly the keyword $this inside orange.phtml refers the class object of it's associated block which is Mynamespace_Mymodule_Block_Orange

So in your case if you copy the content in apple.phtml into orange.phtml, then all the $this keywords in new orange.phtml file will refers to the class object of Mynamespace_Mymodule_Block_Orange. So when you call methods using $this it will invoke them from the class Mynamespace_Mymodule_Block_Orange

Hope you got it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.