繁体   English   中英

Magento Java自定义块

[英]Magento Custom Block for Javascript

使用Magento,我需要在自定义模块中将值从数据库传递到json中的javascript。 由于CSP,我正在避免使用内联JavaScript。 我遵循了http://www.pierrefay.com/magento-create-block-44

我在布局文件中的块的type属性遇到麻烦。 当我使用page / html时,我的javascript模板文件会显示,但会发送text / html标题而不是text / javascript标头,我认为这是xss的风险。

当我尝试使用自定义类型(例如拍卖/历史记录)时,不会加载javascript布局。 而是显示带有标准商店设计的空白页。 以下是我的文件

应用程序/代码/本地/ Mymodule /拍卖/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> 

应用程序/代码/本地/ Mymodule /拍卖/块/History.php

<?php
class Mymodule_Auction_Block_History extends Mage_Core_Block_Template
{
     public function methodblock()
     {
         return ‘informations about my block !!’ ;
     }
}

我认为您误用了Block和控制器。

块只是页面的一部分。 控制器告诉您页面应如何在浏览器中呈现。

关于Block的问题:当您需要创建一个没有任何相关Block方法的新模板时,应使用core/template 此块是最上面的块( html/page继承了core / template,但是添加了您可能不想要的逻辑)

如果您需要一些自定义逻辑(Block方法),请创建一个继承Mage_Core_Block_Template的php Block类。

关于头信息的问题:如果仅输出JSON,则应使用控制器和方法强制输出JSON:

$this->getResponse()->setHeader('Content-type', 'application/x-json');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM