4

I am loading a CSS file via the local.xml this way:

<?xml version="1.0"?>

    <layout>
        <default>
            <reference name="head">
                <action method="removeItem"><type>skin_css</type><name>css/style.css</name></action>
                <action method="addItem"><type>skin_css</type><name>css/style.css</name></action>
            </reference>
        </default>
    </layout>

Only problem is that this file is loaded almost at the beginning of the head. How do I get this file, to be loaded at the end of the header?

5

The default layout handle is applied before any page specific layout handles, so although local.xml updates within a handle are applied after all others, the order of operations for building the layout is as follows:

  1. <default> handle from modules, in module loading order
  2. <default> handle from theme.xml
  3. <default> handle from local.xml
  4. specific handles from modules, in module loading order
  5. specific handles from theme.xml
  6. specific handles handle from local.xml

So you have to add your CSS after all layout XML updates are processed and this is possible with an observer for the controller_action_layout_load_before event. The observer will look like this:

public function addCssAtTheEnd(Varien_Event_Observer $observer)
{  
    $layout = Mage::getSingleton('core/layout');
    $head = $layout->getBlock('head');
    $head->removeItem('skin_css', 'css/style.css');
    $head->addItem('skin_css', 'css/style.css');
}
3

A colleague of mine has found a better solution, so I will post it here:

It seems that Magento 1.9 loads CSS files only last, when they contain an if statement. Instead of the code I used in the local.xml file, he replaced it with this:

   <action method="addItem">
        <type>skin_css</type>
        <name>css/style.css</name>
        <params/>
        <if><![CDATA[<!--[]><!-->]]></if>
    </action>

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.