When you read the class<wbr>Mage_Core_Block_Abstract,you will know that magento creates a very powerful block system.
<div><br></div>
<div>First of all,<wbr>Mage_Core_Block_Abstract is thesubclass of<wbr>Varien_Object. That is to say, magentospecific block class whose parent classis<wbr>Mage_Core_Block_Abstract can add as many childblocks as possible and access them in the array-style
way.</wbr></wbr></wbr>
</div>
<div><br></div>
<div><wbr><wbr> public functionsetChild($alias, $block)</wbr></wbr></div>
<div>
<div><code><wbr><wbr> {</wbr></wbr></code></div>
<div><wbr><wbr><wbr><wbr> if (is_string($block)) {</wbr></wbr></wbr></wbr></div>
<div><wbr><wbr><wbr><wbr><wbr><wbr> $block =$this->getLayout()->getBlock($block);</wbr></wbr></wbr></wbr></wbr></wbr></div>
<div><wbr><wbr><wbr><wbr> }</wbr></wbr></wbr></wbr></div>
<div><wbr><wbr><wbr><wbr> if (!$block) {</wbr></wbr></wbr></wbr></div>
<div><wbr><wbr><wbr><wbr><wbr><wbr> return$this;</wbr></wbr></wbr></wbr></wbr></wbr></div>
<div><wbr><wbr><wbr><wbr> }</wbr></wbr></wbr></wbr></div>
<div><wbr><wbr><wbr><wbr>$block->setParentBlock($this);</wbr></wbr></wbr></wbr></div>
<div><wbr><wbr><wbr><wbr>$block->setBlockAlias($alias);</wbr></wbr></wbr></wbr></div>
<div><wbr><wbr><wbr><wbr> $this->_children[$alias] =$block;</wbr></wbr></wbr></wbr></div>
<div><wbr><wbr><wbr><wbr> return $this;</wbr></wbr></wbr></wbr></div>
<div><wbr><wbr> }</wbr></wbr></div>
</div>
<div><br></div>
<div>Moreover, Magento provides two ways to use block: XML layoutway and object way.</div>
<div><br></div>
<div>1.Use XML layout way</div>
<div><br></div>
<div>
<div><catalog_category_default></div>
<div><wbr><wbr><wbr><wbr> <referencename="left"></wbr></wbr></wbr></wbr></div>
<div><wbr><wbr><wbr><wbr><wbr><blocktype="directory/currency" name="currency"template="directory/currency.phtml"/></wbr></wbr></wbr></wbr></wbr></div>
<div><wbr><wbr><wbr><wbr> </reference></wbr></wbr></wbr></wbr></div>
<div></catalog_category_default></div>
<div><br></div>
</div>
<div>In the template file, use following statement<?php echo$this->getChildHtml("currency");?></div>
<div><br></div>
<div>2.Object way</div>
<div><br></div>
<div><?php</div>
<div>$currency =new<wbr>Mage_Directory_Block_Currency();</wbr>
</div>
<div>echo<wbr><span style="line-height:21px">$currency->toHtml();</span></wbr>
</div>
<div>?></div>
<div><br></div>
</wbr>
Powerful block system in Magento
最新推荐文章于 2025-12-06 15:19:30 发布
本文介绍了Magento中强大的块系统,详细解释了Mage_Core_Block_Abstract类如何作为Varien_Object的子类来实现灵活的块管理和嵌套。同时展示了Magento提供的两种使用块的方式:XML布局方式和对象方式,并提供了具体的代码示例。
1519

被折叠的 条评论
为什么被折叠?



