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?

2 Answers 2

6

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>
2
  • 1
    That's a neat trick, good to know! Sep 1, 2016 at 12:55
  • 1
    Works like a charm! The file gets added right down the bottom of the <HEAD> when the above code is included in a <reference name="head"> block. Totally eliminates the need for store specific includes in local.xml.
    – durzel
    Jul 2, 2019 at 8:58
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');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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