Home » Design » HTML » Access a variable declared in a phtml from another phtml in Magento2

Access a variable declared in a phtml from another phtml in Magento2

posted in: Design, HTML, PHP, Programming, XML 0

Having two blocks in the catalog_product_view.xml file like this:

XML

To access a variable declared in the first block from the second block, you can set the variable with register from the first phtml, then you get the value in the 2nd phtml with registry

To do things well without injecting direclty the objectManager in phtml, you have to create some function your product block, so in your block : app/code/Vendor/Modulename/Block/Custom.phpassuming that your variable name is : $tech add the following code:

PHP

Then in your First phtml :


PHP

Then, in the Second phtml :

PHP

Besides, you can also do that in phtml to test without the block solution,

First phtml :

PHP

Second phtml :

PHP

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.