如何通过magento中的布局xml文件在正文部分(而非标头)中添加Javascript文件 [英] How to add Javascript files in body part (not header) through layout xml files in magento

查看:55
本文介绍了如何通过magento中的布局xml文件在正文部分(而非标头)中添加Javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Magento的初学者,我想通过布局xml文件在正文部分添加javascript文件.

I am beginner in Magento and I want to add javascript file in body section through layout xml file.

<reference name='?????'>
<action method="addJs"><script>js/my_javascript_file.js</script></action>
</reference>

参考名称应该是什么? 我尝试了除"head"之外的其他引用,但它会生成错误.

What should be reference name ? I tried in another references other than "head" but it's generates error..

我在Google上搜索了很多,但没有得到任何解决方案. 是否可以通过xml布局文件将javascript文件添加到正文部分? 我不想将Javascript文件添加到html头部.
此时,我将Javascript文件直接添加到.phtml文件中...

I googled a lot but didn't get any solution about it. Is it possible to add javascript files into body section through layout xml files ? I don't want to add Javascript file into html head part.
At this time I added Javascript files directly into .phtml file...

先谢谢了.

推荐答案

在Magento xml中,action method ="method_name"引用了所引用的块代码中的方法.其他块对象可能未定义"addJs"方法.

In Magento xml, action method="method_name" refers to a method within the referenced block code. Other block objects may not have the "addJs" method defined.

"method_name"是指与阻止核心代码相对应的代码.例如,在app \ code \ core \ Mage \ Page \ Block \ Html \ head.php中:

The "method_name" refers to code that corresponds to that blocks core code. For instance, in app\code\core\Mage\Page\Block\Html\head.php:

public function addJs($name, $params = "")
{
    $this->addItem('js', $name, $params);
    return $this;
}

因此,要将外部js添加到任何其他块中,您将必须编辑基础代码(如果尝试将文件移动到app \ code \ local ...),并添加几种方法才能使其正常工作.


正如他们所说,有多种方法可以为猫皮(不是您想要...).更容易做到的是使用代码创建一个.phtml模板文件,以创建外部链接.

So to add external js to any other block, you would have to edit the underlying code (move file to app\code\local... if you attempt it) and add several methods for this to work correctly.


As they say, there is more than one way to skin a cat (not that you would want to...). What would be easier to do is to make a .phtml template file with code to create the external links.

例如,如果我要使用以下命令制作ext_js.phtml:

For example if I were to make an ext_js.phtml with this:

<?php $_jsExtNames = array('extra1.js' , 'extra2.js'); ?>

<?php foreach($_jsExtNames as $_jsExtName): ?>
    <script src="<?php echo $this->getSkinUrl('js/'.$_jsExtName) ?>"></script>

<?php endforeach; ?>

其中$ _jsExtNames数组是主题skin/js文件夹中的外部js脚本的列表.

Where the $_jsExtNames array is a list of the external js scripts in your themes skin/js folder.

将其添加到XML的问题可能会有所不同,具体取决于添加位置.对于默认区域,如果将.phtml文件放置在page/html文件夹中,则类似的事情会起作用:

The matter of adding it to your XML can change depending on where you are adding it. For default areas, something like this would work if I placed my .phtml file in page/html folder:

<default>
    <reference name="footer">
        <block type="core/template" name="extra_js" template="page/html/ext_js.phtml" />
    </reference>
</default>

单独运行就可以了.如果将其放在另一个块中,则需要在该块的模板文件中调用它.

为了说明该示例,如果我想将外部js放在category视图页面中,可以将其添加到category.xml中,如下所示:

That works just fine on its own. If you place it within another block, you will need to call it within that block's template file.

To illustrate that example, if I wanted to place my external js within the category view page, I would add it to category.xml like so:

<catalog_category_layered translate="label">
    <label>Catalog Category (Anchor)</label>
        <reference name="left">
            <block type="catalog/layer_view" name="catalog.leftnav" after="currency" template="catalog/layer/view.phtml"/>
        </reference>
        <reference name="content">
            <block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml">
                <!-- Added Line Below -->
                <block type="core/template" name="extra_js" template="page/html/ext_js.phtml" />

现在,我们必须通过在文件目录/category/view.phtml中将其添加到的位置添加以下行来按块的名称来调用该块:

Now we'll have to call the block by its name by adding this line where we would like it to go in the file catalog/category/view.phtml:

<?php echo $this->getChildHtml('extra_js'); ?>

那应该行得通,我在键入内容时在安装过程中对其进行了全面测试.

That should work, I was testing it all out on my install while I was typing.

这篇关于如何通过magento中的布局xml文件在正文部分(而非标头)中添加Javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆