Reputation: 24645
I have controller and it has following code:
class Company_ModuleName_NameController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
}
Then I have configured layout XML in my config.xml. Now I would like to add block that points to .phtml file and when user access my controller, that .phtml file would be shown to user.
Upvotes: 1
Views: 3161
Reputation: 3797
First of all you should define layout handle for your controller:
Like this:
<modulename_name_index>
</modulename_name_index>
The you should define where you want to display: content, left, right, another block:
<modulename_name_index>
<reference name="content">
</reference name="content">
</modulename_name_index>
And then you define block which will be displayed:
<modulename_name_index>
<reference name="content">
<block type="module/blockname" name="blockname" template="templatedir/templatefile.phtml"/>
</reference name="content">
</modulename_name_index>
Where module/blockname is the name of your block. In the example file should be like this:
Company/ModuleName/Block/Blockname.php. Your config.xml should be also definded properly so magento understood that with 'module' thing it should load particularly your class. Something like this:
<global>
...
<blocks>
<module>
<class>Company_Module_Block</class>
</module>
</blocks>
...
</global>
UPDATE 1
If you want to simply render some content then you may use Magento's default block class:
core/template or Mage/Core/Block/Template
This is how your xml file then will look like:
<modulename_name_index>
<reference name="content">
<block type="core/template" name="blockname" template="templatedir/templatefile.phtml"/>
</reference name="content">
</modulename_name_index>
BUT if you have in your .phtml file something like:
$this->getSomethingSpecificValueForCurrentDisplay()
This won't work. On another side if you have very easy php in your phtml, like below, you don't need your custom block.
<?php for(i = 0; i++; i< 10){ ?>
<?php echo "Hello mates" ?>
<?php } ?>
Upvotes: 3