How to add JS programmatically in Magento?

后端 未结 3 967
忘了有多久
忘了有多久 2021-01-31 23:53

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\'))         


        
3条回答
  •  醉酒成梦
    2021-02-01 00:08

    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.

    Then in layout XML:

    
    
        
            
                
                    
                    
                
            
        
    
    

提交回复
热议问题