简体   繁体   中英

How load phtml of custom module in magento

I need to know how to load phtml on custom module...

I need to load phtml file on my blocks...in magento

my code is bellow.....I m creating module for my learning

purpose .....

what is the best way to load phtml in custom block...

And if I have model,controller like other mvc...

then what is the need of block in magento..

my controller
-----------
class Packt_New_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
echo "this is my new controller ";
}
    public function newAction(){

        $this->loadLayout();
        $this->renderLayout();

    }


}

my config.xml
----------------
<?xml version="1.0" encoding="UTF-8" ?>
<config>
    <!-- module configuration -->
    <modules>
        <Packt_New>
            <version>0.0.1</version>
        </Packt_New>
    </modules>
    <!-- module configuration end -->
    <global>
        <blocks>
            <new>
                <class>Packt_New_Block</class>
            </new>
         </blocks>
         <helpers>
            <new>
                <class>Packt_New_Helper</class>
            </new> 
        </helpers>
        <models>
            <new>
                <class>Packt_New_Model </class>
            </new>
        </models>
    </global>
    <frontend>
        <routers>
            <new>
                <use>standard</use>
                <args>
                    <module>Packt_New</module>
                    <frontName>new</frontName>  
                </args>
            </new>
        </routers>
        <layout>
        <updates>
            <new>
                <file>new.xml</file>
            </new>
        </updates>
        </layout>
    </frontend>

</config>


my layout.xml
---------------------
<?xml version="1.0" encoding="UTF-8"?>
<layout>
   <!-- <default>
        <remove name="header"/>
    </default>-->
    <new_index_new>
        <refrence name="root">
            <action method="setTemplate">
                <template>page/2columns-right.phtml</template>
            </action>
        </refrence>
        <refrence name="content">
            <block type="new/newproducts" name="block_newproducts" template="new/new.phtml"></block>
        </refrence>
    </new_index_new>
</layout>

this is my block
-------------------
class Packt_New_Block_Newproducts extends Mage_Core_Block_Template
{



}

config.xml like

   <layout>
        <updates>
            <unique_identifier module="YourNameSpace_Test">
                <file>yournamespace_test.xml</file>
            </unique_identifier>
        </updates>
    </layout>

app/design/frontend/default/default/layout/yournamespace_test.xml

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
    <catalog_category_view>
        <reference name="content"> <!-- block name inside which you need to display hello world -->
            <action method="setTemplate">
                <template>yournamespace_test/catalog/yournamespace_test.phtml</template>
            </action>
        </reference>
    </catalog_category_view>
</layout>

More info to go

In magento the layout is little complex. It follows a mvc structure which is slightly different

In other frameworks we have a view which is used by the controller for rendering.

In magento we have different structure. Say a page, its divided into many blocks. This blocks are defined in default handler in layout or custom handler in layout.

new_index_new which you have used is an example of custom handler. So the reference are defined such as head, content. We use to add this handlers to save some reference. This would have the block details.

So, a page generally has various blocks thats needs to render phtml files which are magento template files.

I hope you get your answer. Else please go through some of the blogs in magento

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM