如何通过magento中的布局xml文件在正文部分(不是标题)中添加Javascript文件(How to add Javascript files in body part (not header)

编程入门 行业动态 更新时间:2024-10-25 22:26:45
如何通过magento中的布局xml文件在正文部分(不是标题)中添加Javascript文件(How to add Javascript files in body part (not header) through layout xml files in magento)

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

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

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

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

提前致谢..

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>

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

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...

Thanks in advance..

最满意答案

在Magento xml中,action method =“method_name”指的是引用的块代码中的方法。 其他块对象可能没有定义“addJs”方法。

“method_name”指的是与阻止核心代码相对应的代码。 例如,在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模板文件来创建外部链接。

例如,如果我用这个做一个ext_js.phtml:

<?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脚本的列表。

将其添加到XML中的问题可能会根据您添加它的位置而改变。 对于默认区域,如果我将.phtml文件放在page / html文件夹中,这样的东西就可以了:

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

这本身就好了。 如果将其放在另一个块中,则需要在该块的模板文件中调用它。 为了说明这个例子,如果我想将我的外部js放在类别视图页面中,我会将它添加到category.xml中,如下所示:

<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中的这一行添加它的名称来调用块。

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

这应该工作,我在打字时测试了我的安装。

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.

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; }

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.

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; ?>

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

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>

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" />

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.

更多推荐

本文发布于:2023-08-02 17:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1378733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   布局   标题   正文   magento

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!