dongtuo0828 2013-10-17 01:54
浏览 23
已采纳

Magento扩展:如何在CMS页面或块上输出字符串

I'm trying to create my first Magento extension and I'm stuck at the first step already.

When adding {{block type="rick_printer/print" text="Hello world"}} to a CMS page or block, I want "Hello world" to be displayed. Unfortunately nothing happens. Here's my code:

app\code\local\Rick\Printer\etc\config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config> 
    <modules>
        <Rick_Printer>
            <version>0.0.1</version>
        </Rick_Printer>
    </modules>
    <global>
        <blocks>
            <rick_printer>
                <class>Rick_Printer_Block_Print</class>
            </rick_printer>
        </blocks>
    </global>
</config>

app\etc\modules\Rick_Printer.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Rick_Printer>
            <active>true</active>
            <codePool>local</codePool>
        </Rick_Printer>
    </modules>
</config>

app\code\local\Rick\Printer\Block\Print.php

class Rick_Printer_Block_Print extends Mage_Core_Block_Abstract
{  
    protected function _construct()
    {
        $this->setTemplate('rick/printer/view.phtml');
        parent::_construct();
    }

    public function printIt()
    {
        $text = $this->getText();
        if (!$text) {
            $msg = "Please provide a text!";
            echo $msg;
            return array();
        }        
        return $text;
    }
}

app\design\frontend\default\default\templateick\printer\print.phtml

<?php
    $text = $this->spinIt();
    echo $text;
?>

I know the code is ugly and I'm probably doing it all wrong. Any help is highly appreciated!

Thanks


Update: After applying the fix from Vinai's answer my app\code\local\Rick\Printer\etc\config.xml looks now like this:

<?xml version="1.0" encoding="UTF-8"?>
<config> 
    <modules>
        <Rick_Printer>
            <version>0.0.1</version>
        </Rick_Printer>
    </modules>
    <global>
        <blocks>
            <rick_printer>
                <class>Rick_Printer_Block</class>
            </rick_printer>
        </blocks>
    </global>
</config>

and I'm getting the following error message when accessing the CMS page (which doesn't show):

class Rick_Printer_Block_Print extends Mage_Core_Block_Abstract
{  
protected function _construct()
    {
        $this->setTemplate('rick/printer/view.phtml');  
        parent::_construct();
    }  

    public function printIt()
    {
        $text = $this->getText();  
        if (!$text) {
            $msg = "Please provide a text!";  
            echo $msg; return array();
        }
        return $text;
    }
}


Fatal error: Class 'Rick_Printer_Block_Print' not found in /home/www/xyz/htdocs/app/code/core/Mage/Core/Model/Layout.php on line 491
  • 写回答

1条回答 默认 最新

  • doudanma9706 2013-10-17 08:42
    关注

    First, your block class prefix is wrong. Despite the node name being <class>, what you are actually specifying is a class prefix.
    Another way to look at it is that this node declares the directory, in which the blocks for the module are located.
    The correct way is

    <!-- language: xml -->
    <global>
        <blocks>
            <rick_printer>
                <class>Rick_Printer_Block</class>
            </rick_printer>
        </blocks>
    </global>
    

    Second, in your template you are calling $this->spinIt(); instead of $this->printIt();. Just a typo...

    Otherwise the code looks okay.

    UPDATE The error message indicates a file system path to class name mismatch. Check the casing and for typos.
    Also, since you want your block to render a template, you want to extend Rick_Printer_Block_Print from Mage_Core_Block_Template instead of the _Abstract block class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证