6

I need to add a JS file conditionally and programmatically inside a block file. I tried with these codes:

if (Mage::getStoreConfig('mymodule/settings/enable')) {
$this->getLayout()->getBlock('head')->addJs('path-to-file/file1.js');
} else {
$this->getLayout()->getBlock('head')->addJs('path-to-file/file2.js');
}

However, regardless of what the setting is, none of this file is loaded. I even tried to eliminate the condition and explicitly load one file only, but it still doesn't work. What have I done wrong here?

benmarks
  • 23,384
  • 1
  • 62
  • 84
user1576748
  • 677
  • 3
  • 14
  • 27
  • 1
    Does the conditional statement work for a simple echo/print? – Alana Storm Aug 13 '12 at 15:25
  • Thanks Alan for your reply. Yes, the conditional does provide either 0 or 1 if I echo it out. However, I tried to eliminate the whole lines except one line: $this->getLayout()->getBlock('head')->addJs('path-to-file/file1.js'); But it still doesn't work. The path is after js root directory. – user1576748 Aug 13 '12 at 16:47
  • 1
    Which block's template file are you calling this from? – Alana Storm Aug 13 '12 at 17:33
  • Thanks Alan. It's solved already by comment from benmarks below. – user1576748 Aug 13 '12 at 18:48
  • To answer Alan's question, I'm calling this from the .php block file (the one with capital letter), not the .phtml block file. – user1576748 Aug 13 '12 at 19:04

3 Answers3

20

The issue here is likely one of processing order. My guess is that your PHP code is being evaluated after the head block has been rendered. While your code is successfully updating the head block class instance, it's happening after output has been generated from that instance.

The better solution will be to add the addJs() calls in layout XML so that they will be processed prior to rendering. It would be nice if there were an ifnotconfig attribute, but for now you can use a helper.

Create a helper class with a method which returns the script path based on the config settings, then use this as the return argument.

<?php 
class My_Module_Helper_Class extends Mage_Core_Helper_Abstract
{
    public function getJsBasedOnConfig()
    {
        if (Mage::getStoreConfigFlag('mymodule/settings/enable')) {
            return 'path-to-file/file1.js';
        }
        else {
            return 'path-to-file/file2.js';
        }
    }
}

Then in layout XML:

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="head">
            <action method="addJs">
                <file helper="classgroup/class/getJsBasedOnConfig" />
                <!-- i.e. Mage::helper('module/helper')->getJsBasedOnConfig() -->
            </action>
        </reference>
    </default>
</layout>
benmarks
  • 23,384
  • 1
  • 62
  • 84
  • Excellent! You're rock! It works, except there is a typo error in your code on "if" line. There is unneeded closed bracket prior to curl bracket. Anyway, thank you so much. – user1576748 Aug 13 '12 at 18:46
  • 1
    The dangers of (1) not testing and (2) coding while jet-lagged. Corrected :-) – benmarks Aug 13 '12 at 18:59
5
$this->getLayout()->getBlock('head')->addJs('path');

Its the right code, search if your path is right.

Guerra
  • 2,792
  • 1
  • 22
  • 32
  • Thank you for your reply. Yes, the path is right. It's relative to the root js folder. I even tried to copy my js file to that root js folder (no sub folder) and access it directly like this addJs('file1.js'); – user1576748 Aug 13 '12 at 16:54
  • Try put the code : getJsUrl(); ?>jsFile.js" and see what code he write. The xml addjs is same. – Guerra Aug 13 '12 at 17:06
  • 1
    What if you want to add an absolute path example http://somedomain.com/js/some.js – awavi Apr 02 '14 at 01:24
1

I know this was asked a long time ago, but in case somebody is looking for this, I would suggest to use this in your local.xml:

<layout>
    <default>
        <reference  name="head">
            <action ifconfig="path/to/config" method="addJs">
                <script>pathto/file.js</script>
            </action>
        </reference>
    </default> 
</layout>

Of course this is for JS files located in /js/ folder. Use the appropriate method if you want to add skin_js or skin_css.

PS. Tested on CE 1.9

Razvan
  • 61
  • 3