Str*_*rry 5 magento magento-layout-xml
我正在制作模块的东西是它生成一个javascript片段,所以我不能使用一个动作只是将它挂钩到HTML的部分,因为动作需要我有一个JS文件(如果我是正确的我错误).我有什么方法可以将JavaScript代码段放入代码中?我正在考虑使用一个块,但我不确定该块后面应该添加什么,我必须考虑这将适用于所有主题.
股票头模板是
template/page/html/head.phtml
Run Code Online (Sandbox Code Playgroud)
在您自己的主题中复制该文件将是获取一些javascript的最简单方法.
虽然更好(从开发人员的角度来看),但此模板包含以下行
<?php echo $this->getChildHtml() ?>
Run Code Online (Sandbox Code Playgroud)
about链接打印出块的所有子块.因此,将一个子块添加到头块也可以.
<layouts>
<default> <!-- does this to all pages — use specific layout handles to target a page -->
<reference name="head"> <!-- get a reference to the existing head block -->
<block type="core/text" name="simple_example_javascript_block"> <!-- append a simple text block, probably better to use a new template block -->
<action method="setText"> <!-- set our new block's text -->
<text><![CDATA[
<script type="text/javascript">
alert("foo");
</script>
//]]></text>
</action>
</block>
</reference>
</default>
</layouts>
Run Code Online (Sandbox Code Playgroud)
上面的XML使用一个简单的core/text块来为每个Magento页面添加javascript.作品local.xml,应该在其他地方工作.我相信更好的方法可以考虑到这一点(例如模板块)