3

I need to add a CSS file in local.xml based on some custom variables. I have 5 CSS files but each time only one needs to be loaded.

I only know how to check if a single variable is set in the config. If config returns TRUE, the CSS file will be loaded in the frontend:

<action method="addItem" ifconfig="my_module/settings/variable"><type>skin_css</type><name>css/a.css</name><params/></action>

So for now I must put my code in head.phtml in app\design\frontend\my_theme\default\template\page\html\ to load CSS files like this:

<?php
  $my_var = Mage::helper('my_module/my_helper')->getFileName();
?>
<link href="<?php echo $my_var; ?>.css" rel="stylesheet" type="text/css" media="all" />

But it has a drawback: my CSS files will not be merged by Magento's CSS/JS merge function.

So, is there any way to do the same but inside local.xml? Is there any way to call a method from the helper which would return the name of the CSS file which should be loaded (based on some calculations inside that helper)?

SOLUTION CANDIDATE :)

I tried something similar to what Alan Storm described in this thread: https://stackoverflow.com/a/5601579/1647291

I tried this in my local.xml file:

<action method="addItem">
    <type>skin_css</type>
    <name helper="my_module/my_helper/getFileName" />
    <params/>
</action>

Helper method getFileName() returns file name a.css. But Magento searches for that CSS file inside "base" theme, not in the current theme. Magento loads CSS file with this path:

http://example.com/skin/frontend/base/default/a.css

So my file name is prefixed with example.com/skin/frontend/base/default/. But the correct path should be:

http://example.com/skin/frontend/my_theme/default/a.css

Does anyone know how to achieve this?

7

Bottom line: you need to have a value calculated by PHP, therefore the approach of using a helper is appropriate. I think it makes sense to use the addCss convenience method rather than addItem a it's a little cleaner and should work fine for your needs:

<{handle}>
    <action block="head" method="addCss">
        <stylesheet helper="my_module/my_helper/getFileName" />
    </action>
</{handle}>

Your helper seems to be calculating the path to the CSS files using the design model, but this will be done again by the page/html_head block. Change your helper to simply return the file paths relative to the theme (i.e. not using the design model) and your problem is solved.

  • I'm having this problem too. although my css is in skin/frontend/default/mytheme/css/my.css and I'm returning css/my.css from helper method, still the css was not loaded. Can you post some code snippet from helper method? – Mohammad Faisal Jun 6 '14 at 5:04
  • @MohammadFaisal is your handle being loaded? Is your theme active? – benmarks Jun 9 '14 at 20:17
  • Yes. my theme is active and the method was written in Helper/Data.php and the module is working. – Mohammad Faisal Jun 10 '14 at 4:41
  • And if you include it manually in the <default> handle and just setting it normally by just specifying the path + name does it work? – benmarks Jun 11 '14 at 1:26
  • yes. if in my module's layout xml I add it as: <reference name="head"><action method="addCss"><style>css/my.css</style></action></reference>. this works. – Mohammad Faisal Jun 11 '14 at 4:28
0

I just wanted to let you know that the solution described in my question actually works. I just did a stupid mistake. This code was added to local.xml in my theme:

<action method="addItem">
    <type>skin_css</type>
    <name helper="my_module/my_helper/getFileName" />
    <params/>
</action>

Helper method getFileName() returns file name a.css. But my CSS file wasn't in the correct directory:

http://example.com/skin/frontend/my_theme/default/a.css

so Magento tried to find that file inside "base" theme, not in the current theme:

http://example.com/skin/frontend/base/default/a.css

So if file is in the correct directory, all will work as expected. The code posted by @benmarks will also work:

<action method="addCss">
    <stylesheet helper="my_module/my_helper/getFileName" />
</action>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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