使用local.xml和addlink方法来自定义toplinks

来源:互联网 发布:办公室软件excel教程 编辑:程序博客网 时间:2024/06/09 21:48

以前总是用暴力的方法来自定义top links,方法是直接改links.phtml的html。Links由Mage_Page_Block_Template_Links这个类生成,其实用的是Varien_Object。最近决定更正这个暴力的行为。

首先,local.xml我们应该知道,可以覆盖布局的行为。这个在app/code/core/Mage/Core/Model/Layout/Update.php 的418行可以看到,我用的是magento 1.5.1.0版本。

<?xml version="1.0"?><layout version="0.1.0"><default><reference name="root"><reference name="top.links"><action method="addLink" translate="label title"><label>About Us</label><url>about</url><title>About Us</title><prepare>true</prepare><position>999</position><liParams/><aParams>class="top-link-about-us"</aParams><beforeText>id="alink"</beforeText><afterText>Text</afterText></action></reference></reference></default></layout>

如上的local.xml出来的效果是这样的:


有些奇怪...beforeText和afterText都不太对劲..似乎跟字面意思不符合...需要继续深入地研究内核才行...

如果用这样的xml的话

<action method="removeLinkByUrl"><url helper="customer/getAccountUrl"/></action><action method="addLink" translate="label title" module="customer"><label>My Custom Account</label><url helper="customer/getAccountUrl"/><title>My Account</title><prepare/><urlParams/><position>10</position></action>
出来的结果变成这样:


移除的话你可以使用remove name或者removeLinkByUrl

<action method="removeLinkByUrl"><url helper="customer/getAccountUrl"/></action><remove name="checkout_cart_link"/>

这样对于开发以来toplinks的模块来说,是再灵活不过了..好了,打算开始做基于ajax的login logout,这个应该派得上用场...