【问题标题】:How to add a custom javascript in a Magento extension [duplicate]如何在 Magento 扩展中添加自定义 javascript [重复]
【发布时间】:2015-04-24 19:55:02
【问题描述】:

我正在尝试将 javascript 添加到标记 <head> 内的所有 Magento 页面。我在这个文件夹树中有一个名为example.xml 的文件(我在etc/config.xml 上进行了配置,以使用example.xml 之类的自定义.xml 文件):

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

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

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

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

<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 有一个 sn-p,我需要从 Magento 获取自定义变量(在 php 中我使用 getConfigData('api_key'))。所以,我的第二个问题是:是否可以直接从 javascript 获取这个值,或者我需要用 php 来做这个?

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

【问题讨论】:

  • 请在您的问题中明确说明您遵循了哪些要实现的方法,提供对记录您遵循的方法的 Magento 主页的参考,解释您预期会发生什么以及发生了什么.另外请说明为什么现场现有的问答材料对您不起作用,以及为什么在提供参考时。
  • 唉,请把你的问题分开。 Stackoverflow 不能很好地在同一个问题帖子中提出两个或多个问题。而是将问题分开,尽可能孤立地提问,以便为这次问答创造最大价值。

标签: javascript php xml magento magento-1.9


【解决方案1】:

因为你想执行 php 代码然后需要调用 phtml 这个调用phtml文件作为head的引用块。

<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中。

然后在head.phtml中调用&lt;?php echo $this-&gt;getChildHtml('myjavascript');?&gt;

【讨论】:

    猜你喜欢
    • 2016-06-18
    • 2014-01-23
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    相关资源
    最近更新 更多