Explanation regarding phtml coding in magento 2
-
29-09-2020 - |
Question
When I explored the header links and footer links inside the files of Magento 2, the links are declared like each in one seperate default.xml
file inside individual modules. But the links are displayed in same block at frontend as list elements.
The phtml
coding is different from Magento 1.9.x. My doubt is how they are grouping the seperately declared blocks (in default.xml
) and displaying in frontend as a single block elements (list) using phtml
files.
Can someone please explain the concept in easily understandable way. Am a beginner in php coding and newbie to Magento 2.
Explanation with a sample phtml file example:footer.phtml
is most welcomed.
Solution
First of all, footer.phtml file doesn't seem to be used anywhere, just in single Test class.
But speaking about concept, this is pretty easy :) You can create single block, with single .phtml template file. And by using referenceBlock in other modules you are still working with the same block.
In this situation we have block with Magento\Framework\View\Element\Html\Links
class and footer_links
name.
In other modules we can add more links to this block by using <referenceBlock name="footer_links">
and those links will be visible in previously created block. Simple as it. Hope you understood my explaination :)