嗨,
默认情况下,Magento 不允许您这样做,为了能够以特定顺序(标题中的 css 或 javascript)添加文件,您必须自定义核心。
创建一个自定义模块,在其中重写 page/html_head 块:
<?xml version="1.0"?>
<config>
<modules>
<Koncz_Utility>
<version>1.0.0</version>
</Koncz_Utility>
</modules>
<global>
<blocks>
<class>Koncz_Utility_Block</class>
<page>
<rewrite>
<html_head>Koncz_Utility_Block_Page_Html_Head</html_head>
</rewrite>
</page>
</blocks>
</global>
</config>
并添加 2 个自定义函数:addItemFirst和addItemAfter
class Koncz_Utility_Block_Page_Html_Head extends Mage_Page_Block_Html_Head {
/*
* Adds a head element into the first position, usage same as for addItem ;)
*/
public function addItemFirst($type, $name, $params = null, $if = null, $cond = null) {
if ($type === 'skin_css' && empty($params)) {
$params = 'media="all"';
}
$firstElement = array();
$firstElement[$type . '/' . $name] = array(
'type' => $type,
'name' => $name,
'params' => $params,
'if' => $if,
'cond' => $cond,
);
$this->_data['items'] = array_merge($firstElement, $this->_data['items']);
return $this;
}
/*
* Adds a custom css, or js file after the $type, if it was found! $type=js/jquery/jquery.js <= or skin_js/path
*/
public function addItemAfter($after, $type, $name, $params = null, $if = null, $cond = null) {
if ($type === 'skin_css' && empty($params)) {
$params = 'media="all"';
}
$firstElement = array();
$firstElement[$type . '/' . $name] = array(
'type' => $type,
'name' => $name,
'params' => $params,
'if' => $if,
'cond' => $cond,
);
if (array_key_exists($after, $this->_data['items'])) :
// get the position
$pos = 1;
foreach ($this->_data['items'] as $key => $options) :
if ($key == $after) :
break;
endif;
$pos +=1;
endforeach;
array_splice($this->_data['items'], $pos, 0, $firstElement);
endif;
return $this;
}
}
在您的 local.xml 之后,您可以使用以下内容:(这将在第一个位置添加 jquery.js,在第二个位置添加 jquery 之后的 jquery.no-conflict.js)。{{ 另外值得一提的是,您的 skin_js 文件将在 js 类型之后添加!所以使用 js 而不是 skin_js 作为基础 js 文件。
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
<update handle="add_jquery_elegant_way" />
</default>
<add_jquery_elegant_way>
<reference name="head">
<action method="addItemFirst"><type>js</type><script>jquery/jquery.js</script></action>
<action method="addItemAfter">
<after>js/jquery/jquery.js</after>
<type>js</type>
<script>jquery/jquery.no-conflict.js</script>
</action>
</reference>
</add_jquery_elegant_way>
</layout>