문제

I would like to understand if a block is left in an xml file but the call to that block in a template file is removed, does the xml still get processed but never rendered?

Example:

In base/default/layout/page.xml there is a block within header called top.links. That block is called in base/default/page/header.phtml.

If I remove the call to the block from header.phtml in my own theme, but don't also remove the xml, does top.links get processed anyway.

Wondering if there could be a performance hit caused by doing this.

도움이 되었습니까?

해결책

Here's a high level view of how the layout is loaded

  • Magento loads and merges all the layout XML files from the file system into one giant XML tree. This tree is cached if caching is on. This tree is called the "package layout"

  • Magento then looks through the list of "handles" issued for a particular request, and picks out the bits of XML from the package layout that match these handles. These results are merged into a single XML tree called the page layout

  • Then, Magento processes the page layout, instantiating a block object for each <block/> found, calling action methods, etc.

  • Then, Magento calls the toHtml method on the root block. If the root block's template contains getChildHtml method calls, Magento loads the block named in getChildHtml and calls its toHtml method. This continues all the way down.

  • The results of the above step are a string, which Magento sets as the body of the response object

  • One of the last actions of the Magento system code is to output the body of the response object back to the browser (or other output context).

So in your example, removing the call to getChildHtml means the block's toHtml method is never called, so you save yourself that processing. However, the block itself is still instantiated, meaning you do not save yourself that processing. The rendering of all those blocks is a significant reason for Magento's infamous performance profile, and why there are so many output caching strategies.

If you're interested in the full details of the rendering I described, my book No Frills Magento Layout covers the entire thing in simple, easy to follow chapters that build on one another.

다른 팁

The method toHtml is not called in your case. Usually that's where the heavy logic occurs because that's where the template is rendered, but the block class still gets instantiated.
For more details on how it works investigate Mage_Core_Model_Layout::_generateBlocks() method that is called when the layout is loaded.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top