创建一个新块并将其添加到模板中

时间:2018-05-22 16:19:56

标签: magento magento-1.8

我是Magento的初学者,需要帮助创建一个新的自定义块。 基本上我只是希望块显示"你好"如果它被调用。

  1. 模块安装xml文件, app / etc / modules / MyExtensions_HelloBlock.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
        <modules>
            <MyExtensions_HelloBlock>
                <active>true</active>
                <codePool>local</codePool>
            </MyExtensions_HelloBlock>
        </modules>
    </config>
    
  2. 模块配置xml文件, app / code / local / MyExtensions / HelloBlock / etc / config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
        <modules>
            <MyExtensions_HelloBlock>
                <version>0.0.1</version>
            </MyExtensions_HelloBlock>
        </modules>
        <global>
            <blocks>
                <helloblock>
                    <class>MyExtensions_HelloBlock_Block</class>
                </helloblock>
            </blocks> 
        </global>
    </config>
    
  3. 阻止类, app / code / local / MyExtensions / HelloBlock / Hello.php

    <?php
    class MyExtensions_HelloBlock_Block_Hello extends Mage_Core_Block_Template 
    {    
        public function hello()
        {
            echo "hello";
        }
    }
    ?>
    
  4. 块的模板文件, app / design / frontend / default / default / template / helloblock / hello.phtml

    <?php
        $this->hello();
    ?>
    
  5. 然后我在模板中调用我的新块#34; app / design / frontend / venedor / default / template / page / 1column.phtml&#34;:

    echo $this->getLayout()->createBlock('helloblock/hello')->setTemplate('helloblock/hello.phtml')->toHtml();
    

    结果:

      

    致命错误:在布尔值中调用成员函数setTemplate()   /app/design/frontend/venedor/default/template/page/1column.phtml   在第58行

    我关注此tutorial

1 个答案:

答案 0 :(得分:1)

阻止文件夹

你的街区路径应为 app / code / local / MyExtensions / HelloBlock / Block / Hello.php