2015-04-24 45 views
1

我试图将JavaScript添加到标签<head>内的所有Magento页面。我有一个名为在这个example.xml文件夹树(我做了上etc/config.xml配置为使用自定义的.xml文件像example.xml):如何在Magento扩展中添加自定义JavaScript

--design 
----frontend 
------base 
--------default 
----------layout 
------------example.xml 

我使用两种方法来做到这一点。第一个是(不工作):

<layout> 
<default> 
    <reference name="head"> 
     <action method="addJs"> 
      <script>folder/myjavascript.js</script> 
     </action> 
    </reference> 
</default> 
</layout> 

第二个是(仅用于结帐页面WORKING):

<layout> 
    <example_handle> 
     <reference name="head"> 
      <action method="addJs"> 
       <script>folder/myjavascript.js</script> 
      </action> 
     </reference> 
    </example_handle> 

    <checkout_onepage_index> 
     <update handle="example_handle"/> 
    </checkout_onepage_index> 

    <onepagecheckout_index_index> 
     <update handle="example_handle"/> 
    </onepagecheckout_index_index> 

    <opc_index_index> 
     <update handle="example_handle"/> 
    </opc_index_index> 

    <aw_onestepcheckout_index_index> 
     <update handle="example_handle"/> 
    </aw_onestepcheckout_index_index> 
</layout> 

我的JavaScript有一个片段,我需要从Magento的自定义变量(在php中我使用getConfigData('api_key'))。所以,我的第二个问题是:是否有可能直接从JavaScript获得这个值,或者我需要用PHP来做到这一点?

var _api_key = ?`getConfigData('api_key')`?; 
+0

请你的问题中明确实现其许多mehtods您已紧随其后,提供参考Magento的主页是记录你遵循的方法,说明你期望发生而是发生了什么。同时请说明为什么现场的问答材料为什么不适合您,以及为什么要提供参考资料。 – hakre

+0

Alao请保持你的问题分开。在同一问题帖子中,Stackoverflow不能很好地询问两个或更多问题。相反,保持问题分开,并要求尽可能孤立地为这个问答创造最大的价值。 – hakre

回答

0

当您要执行的PHP代码则需要调用PHTML 这个调用PHTML文件头的参考块。

<example_handle> 
    <reference name="head"> 
     <block type="core/template" template="page/myjavascript.phtml" name="myjavascript" /> 
    </reference> 
</example_handle> 

然后PHTML(app/design/frontend/YOUR_PACKAGE/YOUR_TEMPATE/template/page/myjavascrpt.phtml)编写PHP代码,然后放myjavascript.js的所有代码到这个PHTML。

然后调用<?php echo $this->getChildHtml('myjavascript');?>head.phtml

相关问题