使用 Magento,我需要在自定义模块中将值从数据库传递到 json 中的 javascript。由于 CSP,我正在避免使用内联 javascript。我关注了 http://www.pierrefay.com/magento-create-block-44
我在布局文件中的块的类型属性方面遇到问题。当我使用 page/html 时,我的 javascript 模板文件会显示,但会发送一个 text/html 标头而不是我认为存在 xss 风险的 text/javascript。
当我尝试自定义类型(如拍卖/历史)时,javascript 布局不会加载。相反,它显示一个带有标准商店设计的空白页面。以下是我的文件
app/code/local/Mymodule/Auction/etc/config.xml
在这里,除了我尝试用于 javascript 的新块之外,我在块中还有一个自定义产品视图页面。
<config>
...
<frontend>
<routers>
<routeurfrontend>
<use>standard</use>
<args>
<module>Mymodule_Auction</module>
<frontName>auction</frontName>
</args>
</routeurfrontend>
</routers>
<layout>
<updates>
<auction>
<file>history.xml</file>
</auction>
</updates>
</layout>
</frontend>
<global>
<blocks>
<Mymodule_Auction>
<class>Mymodule_Auction_Block_Catalog_Product_View</class>
</Mymodule_Auction>
<catalog>
<rewrite>
<product_view>Mymodule_Auction_Block_Catalog_Product_View</product_view>
</rewrite>
</catalog>
<auction>
<class>Mymodule_Auction_Block</class>
</auction>
</blocks>
... (code for models and helpers) ...
</global>
</config>
app/design/frontend/Mymodule/default/layout/history.xml 如果我将 type="auction/history" 更改为 "page/html" 它会显示但带有 text/html 标题而不是 text/javascript
<layout version="0.1.0">
<default>
<reference name="content"></reference>
</default>
<auction_index_history>
<reference name="content">
<block type="auction/history" template="auction/history.phtml" />
</reference>
</auction_index_history>
</layout>
app/code/local/Mymodule/Auction/Block/History.php
<?php
class Mymodule_Auction_Block_History extends Mage_Core_Block_Template
{
public function methodblock()
{
return ‘informations about my block !!’ ;
}
}