Reputation: 13923
I want to create a block and add that block to my template via
$this->_addContent($this->getLayout()->createBlock("device/device"))
Right now, it is not displaying anything.
What are the points to be Noted so that my block will get rendered (what are the files to be aware off?)
Note:
COMPANY NAME: Abc
MODULE NAME: Device
Also, createBlock("device/device") returns "false"
Upvotes: 1
Views: 1559
Reputation: 5908
The device/device
string that is being passed to createBlock
is a class alias. Class aliases give the Magento developer a way to refer to classes without using the actual class name. This indirection allows for one class to be substituted (or rewritten in Magento terminology) for another without having to change any code which instantiates and uses the class.
You start by defining the prefix for your classes in your module's config.xml
file as follows (note: add this code into any existing tags, rather than just dropping it in at the bottom of the config.xml
):
<config>
<global>
<blocks>
<device>
<class>Abc_Device_Block</class>
</devicer>
</blocks>
</global>
</config>
When building the class name for a block, the part of the xml is the part which comes before the / in the alias, and is substituted for the contents of the tags when generating the class name. The / is then replaced with an _ and the remaining part of the class alias is appended to the class name. So with the class alias device/device
, and the above XML, the following class name will be built Abc_Device_Block_Device
, which Magento will expect to find in Abc/Device/Block/Device.php
. It will search folders in the order specified in the include_path, which is normally app/code/local
, then app/code/community
, followed by app/code/core
and finally `lib.
The same basic logic also applies to model and helper classes.
Alan Storm's indispensable CommerceBug extension has a great tool for testing what model/block/helper class aliases map to in terms of class names and file locations.
The other parameters on this method are a name which can be used to refer to the block (and modify it) from layout XML files, and an array of other attributes which could be found in the layout XML.
Upvotes: 9
Reputation: 401
That was an excellent answer Jim. Adding a point to it, the priority is the
Upvotes: 2