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 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度