This is a basic question, but which drove me sleepless for the past a few days.
I wrote a customized module with a controller, a phtml template and a block class as below: app/code/local/Huahan/HelloWorld/Block Helloworld.php (where Huahan_Helloworld_Block_Helloworld extends Mage_Core_Block_Template) app/code/local/Huahan/HelloWorld/controllers IndexController.php (where indexAction of index controller is defined) app/design/frontend/base/default/template/huahan/helloworld helloworld.phtml (where view is defined) app/design/frontend/base/default/layout helloworld.xml (the filename "helloworld.xml" is specified in etc/config.xml)
I know that every request handling in Magento starts from a controller and its action method.
In the layout helloworld.xml, I specifies the template file in the block tag
<helloworld_index_nihao>
<block type="core/template" name="just_an_arbitary_name" output="toHtml" template="huahan/helloworld/nihao.phtml"/>
</helloworld_index_nihao>
</layout>
So the controller knows which template to use while handling request.
The problem is how I can let Magento know that Huahan_Helloworld_Block_Helloworld is the block class I want it to load before rendering any output?
And if there is any detailed documentation about the Magento xml? I am always frustrated by the complex and arbitrariness of the Magento xml syntax
The version of Magento is ver.1.9.0.1
The config.xml is as below
<?xml version="1.0"?>
<config>
<modules>
<huahan_helloworld>
<version>
0.1.0
</version>
</huahan_helloworld>
</modules>
<frontend>
<routers>
<!-- the <helloworld> tagname appears to be arbitrary, but by
convention is should match the frontName tag below-->
<helloworld>
<use>standard</use>
<args>
<module>Huahan_HelloWorld</module>
<frontName>helloworld</frontName>
</args>
</helloworld>
</routers>
<layout>
<updates>
<helloworld>
<file>helloworld.xml</file>
</helloworld>
</updates>
</layout>
</frontend>
<global>
<blocks>
<helloworld>
<class>Huahan_Helloworld_block</class>
</helloworld>
</blocks>
</global>
</config>
Thanks in advance. I really appreciate that you read it down here.
I figured it out.
The layout xml file is where I should work.
Just change the value of type="core/template" in block item of helloworld.xml to type="Huahan_Helloworld_Block_Helloworld"
then it is done!
I hate Magento, "core/template" does not look like a class name from any view angle!
The block type
attribute is the item which configures what class a block uses. Using a full class-name in the type
although possible is discouraged.
The purpose of the class-alias system in Magento is to allow you the flexibility if extending/overwriting core/module functionality in your own modules.
If everyone uses the class-alias you can then replace/override an alias with your own classes and "inject" your behavior instead of the original. It's a form of decoupling code to allow extendability.
In your module declaration you already had to specify a block prefix such as:
<blocks>
<huahan_helloworld>
<class>Huahan_Helloworld_Block</class>
</huahan_helloworld>
</blocks>
According to Magento conventions the <class>
part should be either huahan_helloworld
or helloworld
.
Because of these configurations and other Magento conventions your block type should be huahan_helloworld/helloworld
or helloworld/helloworld
(respectively).