Powerful block system in Magento

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-&gt;getLayout()-&gt;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-&gt;setParentBlock($this);</wbr></wbr></wbr></wbr></div> <div><wbr><wbr><wbr><wbr>$block-&gt;setBlockAlias($alias);</wbr></wbr></wbr></wbr></div> <div><wbr><wbr><wbr><wbr> $this-&gt;_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>&lt;catalog_category_default&gt;</div> <div><wbr><wbr><wbr><wbr> &lt;referencename="left"&gt;</wbr></wbr></wbr></wbr></div> <div><wbr><wbr><wbr><wbr><wbr>&lt;blocktype="directory/currency" name="currency"template="directory/currency.phtml"/&gt;</wbr></wbr></wbr></wbr></wbr></div> <div><wbr><wbr><wbr><wbr> &lt;/reference&gt;</wbr></wbr></wbr></wbr></div> <div>&lt;/catalog_category_default&gt;</div> <div><br></div> </div> <div>In the template file, use following statement&lt;?php echo$this-&gt;getChildHtml("currency");?&gt;</div> <div><br></div> <div>2.Object way</div> <div><br></div> <div>&lt;?php</div> <div>$currency =new<wbr>Mage_Directory_Block_Currency();</wbr> </div> <div>echo<wbr><span style="line-height:21px">$currency-&gt;toHtml();</span></wbr> </div> <div>?&gt;</div> <div><br></div> </wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值