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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With