<div class="header-container"> <div class="header"> <?php if ($this->getIsHomePage()):?> <h1 class="logo"><strong><?php echo $this->getLogoAlt() ?></strong><a href="<?php echo $this->getUrl('') ?>" title="<?php echo $this->getLogoAlt() ?>" class="logo"><img src="<?php echo $this->getLogoSrc() ?>" alt="<?php echo $this->getLogoAlt() ?>" /></a></h1> <?php else:?> <a href="<?php echo $this->getUrl('') ?>" title="<?php echo $this->getLogoAlt() ?>" class="logo"><strong><?php echo $this->getLogoAlt() ?></strong><img src="<?php echo $this->getLogoSrc() ?>" alt="<?php echo $this->getLogoAlt() ?>" /></a> <?php endif?> <div class="quick-access"> <?php echo $this->getChildHtml('topSearch') ?> <p class="welcome-msg"><?php echo $this->getWelcome() ?> <?php echo $this->getAdditionalHtml() ?></p> <?php echo $this->getChildHtml('topLinks') ?> <?php echo $this->getChildHtml('store_language') ?> </div> <?php echo $this->getChildHtml('topContainer'); ?> </div> </div> <?php echo $this->getChildHtml('topMenu') ?>
这个模板上涉及到的方法:
class Mage_Page_Block_Html_Header extends Mage_Core_Block_Template { public function _construct() { $this->setTemplate('page/html/header.phtml'); } /** * Check if current url is url for home page * * @return true */ public function getIsHomePage() { //getUrl('')返回首页连接 return $this->getUrl('') == $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true)); } public function setLogo($logo_src, $logo_alt) { //PHP的奇葩,_data['logo_src']= $logo_src $this->setLogoSrc($logo_src); $this->setLogoAlt($logo_alt); return $this; } public function getLogoSrc() { if (empty($this->_data['logo_src'])) { $this->_data['logo_src'] = Mage::getStoreConfig('design/header/logo_src'); } //获取配置名,然后到皮肤文件夹中寻找 return $this->getSkinUrl($this->_data['logo_src']); } public function getLogoAlt() { if (empty($this->_data['logo_alt'])) { $this->_data['logo_alt'] = Mage::getStoreConfig('design/header/logo_alt'); } return $this->_data['logo_alt']; } public function getWelcome() { if (empty($this->_data['welcome'])) { //如果已经登录 if (Mage::isInstalled() && Mage::getSingleton('customer/session')->isLoggedIn()) { $this->_data['welcome'] = $this->__('Welcome, %s!', $this->escapeHtml(Mage::getSingleton('customer/session')->getCustomer()->getName())); } else { $this->_data['welcome'] = Mage::getStoreConfig('design/header/welcome'); } } return $this->_data['welcome']; } }
这里对应了后台的三个设置:(System->Configuration->Design->Header)
可以给定Logo图片,图片的Alt属性,已经显示的Welcome文本。
getUrl(”)是从父类继承过来的,它可以获取首页连接。
1 子块topSearch
<reference name="header"> <block type="core/template" name="top.search" as="topSearch" template="catalogsearch/form.mini.phtml"/> </reference>
这个搜索块的类型是core/template,那不就是指Mage_Core_Block_Template吗,而这个类一般作为块的父类,但是不一定都去继承它。这个toSearch模板事实是使用到Mage_Catalogsearch_Helper_Data助手类,但是模板是必须对应一个块的,所有就有了这个core/template作为基础类型。
2 子块topLinks
<default> <reference name="top.links"> <action method="addLink" translate="label title" module="customer"> <label>My Account</label> <url helper="customer/getAccountUrl"/> <title>My Account</title> <prepare/> <urlParams/> <position>10</position> </action> </reference> </default> <customer_logged_in> <reference name="top.links"> <action method="addLink" translate="label title" module="customer"> <label>Log Out</label> <url helper="customer/getLogoutUrl"/> <title>Log Out</title> <prepare/> <urlParams/> <position>100</position> </action> </reference> </customer_logged_in> <customer_logged_out> <reference name="top.links"> <action method="addLink" translate="label title" module="customer"> <label>Log In</label> <url helper="customer/getLoginUrl"/> <title>Log In</title> <prepare/> <urlParams/> <position>100</position> </action> </reference> <remove name="reorder"/> </customer_logged_out> <default> <reference name="top.links"> <block type="checkout/links" name="checkout_cart_link"> <action method="addCartLink"/> <action method="addCheckoutLink"/> </block> </reference> </default> <default> <reference name="top.links"> <block type="wishlist/links" name="wishlist_link"/> <action method="addLinkBlock"> <blockName>wishlist_link</blockName> </action> </reference> </default>
以上是从包布局中找出来的针对top.links的操作。可以看到,在default句柄中,执行了四个方法。还有customer_logged_in 和 customer_logged_out就让人困惑了。可以说,这两个句柄完全是为了配合客户是否已经登录对布局做不同的修改而添加的,如果客户已经登录,句柄customer_logged_in总是会被添加,如果没有登录customer_logged_out就总是被添加。
根据这个状态,应用不同的布局。
这个块的类型是page/template_links,对应的类是Mage_Page_Templete_Links,看看构造函数:
protected function _construct() { $this->setTemplate('page/template/links.phtml'); }
它使用了page/template/links.phtml作为模板文件:
<?php $_links = $this->getLinks(); ?> <?php if(count($_links)>0): ?> <ul class="links"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>> <?php foreach($_links as $_link): ?> <?php if ($_link instanceof Mage_Core_Block_Abstract):?> <?php echo $_link->toHtml() ?> <?php else: ?> <li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li> <?php endif;?> <?php endforeach; ?> </ul> <?php endif; ?>
这段代码很简单,获取所有连接,然后循环。对于每个_link,都可以是Mage_Core_Block_Abstract类型的Block。 仔细看下,有些是执行了addLink有些是执行了addLinkBlock,如果是addLinkBlock就是添加了一个Mage_Core_Block_Abstract类型的Block,那么输出时只要直接调用toHtml()就可以。
不过这里的addLink可比较有学问了:
public function addLink($label, $url='', $title='', $prepare=false, $urlParams=array(), $position=null, $liParams=null, $aParams=null, $beforeText='', $afterText='') { if (is_null($label) || false===$label) { return $this; } $link = new Varien_Object(array( 'label' => $label, 'url' => ($prepare ? $this->getUrl($url, (is_array($urlParams) ? $urlParams : array())) : $url), 'title' => $title, 'li_params' => $this->_prepareParams($liParams), 'a_params' => $this->_prepareParams($aParams), 'before_text' => $beforeText, 'after_text' => $afterText, )); $this->_links[$this->_getNewPosition($position)] = $link; if (intval($position) > 0) { ksort($this->_links); } return $this; }
仔细对照如下代码就能明白这里的参数的作用:
<li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
LiParams表示在li标签中添加的属性。BeforeText表示在a标签之前输出的内容。Url资源就是a标签的链接了。Title对应a标签的title属性。AParams对应a标签的属性。Label就是a标签的描文本(必须)。AfterText就是a标签之后要输出的内容。
3 store_language
4 catalog.topnav
<?php $_menu = $this->getHtml('level-top')?> <?php if($_menu): ?> <div class="nav-container"> <ul id="nav"> <?php echo $_menu ?> </ul> </div> <?php endif ?>
这个目录的输出看起来非常简单,但是如果看看它的实现就比较复杂了。
5 top.container
这个块作为header的最后一个内容,实际上它是头部的一个容器,我们可以把内容放到这里面。
作为一个例子:
<default> <reference name="top.container"> <block type="customer/form_register" name="customer_form_register" template="customer/form/register.phtml"/> </reference> </default>
那么头部最后将输出注册表单。
永久链接:http://blog.ifeeline.com/518.html
原创文章,转载务必保留出处。