magento:将变量从block或hlper获取到javascript文件中
我通过布局xml包含一个js文件: 例如:magento:将变量从block或hlper获取到javascript文件中,magento,Magento,我通过布局xml包含一个js文件: 例如: <reference name="head"> <action method="addItem"> <type>js</type> <name>myjs.js</name> </action> </reference> js myjs.js 这很好。但是,我正在myjs.js内设置一个ajax调用,并希望
<reference name="head">
<action method="addItem">
<type>js</type>
<name>myjs.js</name>
</action>
</reference>
js
myjs.js
这很好。但是,我正在myjs.js内设置一个ajax调用,并希望从帮助器类中提取post url
这怎么可能呢?在您对
头的引用中,我将添加一个块,该块将输出一个部分,该部分将在添加myjs.js文件之前设置一个js var
例如:
<reference name="head">
<block type="page/html" name="mycustomblock" template="page/html/mycustomblock.phtml"/>
<action method="addItem">
<type>js</type>
<name>myjs.js</name>
</action>
</reference>
js
myjs.js
您的文件mycustomblock.phtml将包含:
<?php
// below is javascript that is being populated by your helper
?>
<script> var myblogpost = <?php echo Mage::helper('mycustom/helper')->blogPostUrl();?>; </script>
var myblogpost=;
是。我建议这样做。如果你想在JS文件中包含PHP变量,你必须解析这些文件,那么你不能轻易地缓存它们,客户端也无法访问它们,你在内容服务器、CDN,。。。