Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add css to head from template file in Magento

Tags:

css

magento

I want to add a CSS file inside tag from template (.phtml) file in magento. Is it possible ?

There is a reason to do so: the CSS file name is dynamic, so I don't know until the template executes. Is it possible to do so?

like image 713
Sohail Avatar asked Nov 30 '22 06:11

Sohail


1 Answers

To add a CSS file from a controller after you've loaded the layout, but before you've rendered the layout, you'd do something along the lines of:

public function indexAction() {
    $this->loadLayout();

    $head = Mage::app()->getLayout()->getBlock('head');
    $head->addItem('skin_css', 'css/additional.css');

    $this->renderLayout();
}

The problem with doing something this this in the template file is that it's highly likely that the head template has already been rendered, and so the additional directives you give the block instance are useless because they are too late.

Just use a layout file and do the following:

<?xml version="1.0">
<layout>
    <default>
        <reference name="head">
            <action method="addItem"><type>skin_css</type><file>css/additional.css</file></action>
        </reference>
    </default>
</layout>
like image 164
Nick Avatar answered Dec 05 '22 20:12

Nick