创建一个新块并将其添加到模板
Create a new block and add it to a template
我是 Magento 新手,需要有关创建新自定义块的帮助。
基本上我只希望块在调用时显示 "hello"。
模块安装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>
模块配置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>
屏蔽class,app/code/local/MyExtensions/HelloBlock/Hello.php
<?php
class MyExtensions_HelloBlock_Block_Hello extends Mage_Core_Block_Template
{
public function hello()
{
echo "hello";
}
}
?>
块的模板文件,app/design/frontend/default/default/template/helloblock/hello.phtml
<?php
$this->hello();
?>
然后我在模板中这样调用我的新块"app/design/frontend/venedor/default/template/page/1column.phtml":
echo $this->getLayout()->createBlock('helloblock/hello')->setTemplate('helloblock/hello.phtml')->toHtml();
结果:
Fatal error: Call to a member function setTemplate() on boolean in
/app/design/frontend/venedor/default/template/page/1column.phtml
on line 58
我正在关注这个 tutorial。
阻止 文件夹丢失
你的块的路径应该是 app/code/local/MyExtensions/HelloBlock/Block/Hello.php
我是 Magento 新手,需要有关创建新自定义块的帮助。 基本上我只希望块在调用时显示 "hello"。
模块安装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>
模块配置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>
屏蔽class,app/code/local/MyExtensions/HelloBlock/Hello.php
<?php class MyExtensions_HelloBlock_Block_Hello extends Mage_Core_Block_Template { public function hello() { echo "hello"; } } ?>
块的模板文件,app/design/frontend/default/default/template/helloblock/hello.phtml
<?php $this->hello(); ?>
然后我在模板中这样调用我的新块"app/design/frontend/venedor/default/template/page/1column.phtml":
echo $this->getLayout()->createBlock('helloblock/hello')->setTemplate('helloblock/hello.phtml')->toHtml();
结果:
Fatal error: Call to a member function setTemplate() on boolean in /app/design/frontend/venedor/default/template/page/1column.phtml on line 58
我正在关注这个 tutorial。
阻止 文件夹丢失
你的块的路径应该是 app/code/local/MyExtensions/HelloBlock/Block/Hello.php