2

I am a newbie in Magento and its very confusing. I read files for view come from Layout folder , then i read somewhere that view is managed by files in Block folder.What is true ? Its totally confusing.If there is PHTML file for view (HTML) , what is purpose of layout XML file ??

Thanks !

2 Answers 2

5

A block is a PHP class. This class does the work. This work might be to render a template (phtml). PHTML files are living in

/app/design/frontend/<package>/<theme>/<whatever>/<you>/<want>/<yourfile>.phtml

Layout-XML files are to manage the structure.

All your blocks are organized in a tree structure and you can add nodes (more or less) wherever you want.

So normally you take a block already exists <reference name="content"> and inside this refence node, you add a block

and_more_things_if_your_class_supports_it can for example be a template.

2
  • But why it is so complicated ? I learned cakePHP , it was cool , but this Magento stuff is very confusing and having Steep learning curve! Commented Jan 25, 2015 at 18:07
  • 1
    I have no clue how cakePHP is doing stuff, but you want to separate code and html, therefore you need a block class and a template. And you want be able to add easily modules, therefore you need some mechanism which allows you to add new blocks. All layout XMLs are merged together, so you are able to add new blocks to your existing layout Commented Jan 25, 2015 at 19:20
4

Magento implements MVC differently compared to other MVC frameworks .

In magento View part of MVC is separated into Layouts and blocks.

Layouts defines what a page should contain and how different nodes are related. Every Block in the block folder has a corresponding phtml file(which is declared in layout file). Block job is to load corresponding phtml file and provide some public function for it.

When Magento receives a page request , for example catalog page. it reads the Layout file and finds out what are all the block it should render for that request.for example

<block type="page/html" name="root" template="page/1column.phtml" />

Magento will read this chunk of xml and find out that it should instantiate Mage_Page_Block_Html class and pass the template file it is responsible for.

Magento is confusing sometimes.but once you use to it.You will just love it.

4
  • Thanks .I wish you keep with me in contact , I need some help for magento.Check my profile page Commented Feb 5, 2015 at 8:00
  • Happy to help you. Commented Feb 5, 2015 at 8:26
  • Hope you will Update your profile with email address or some contact link.Thanks Commented Feb 5, 2015 at 8:40
  • 1
    I have updated my profile. Commented Feb 5, 2015 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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