Load Magento module template

2019-09-19 13:06发布

问题:

For a Magento module I need to load template file and let it replace the complete page. It is a XML file (but could also be any content whatsoever). Generally speaking when

MYNS_MYMODULE_controllernameController is triggered and calls fooAction() I need to be able to display a clean site with the content from my template file.

Please let me know where to place the template file and how to tell Magento to load this file as a root template without anything else around.

Edit, to clarify it more:

For http://domain.tld/modulename/controller/action/

Where do I have to place template files and how should I reference them?

回答1:

You could do it like this

  $this->loadLayout()->getLayout()->getBlock('root')->setTemplate('page/1column.phtml');
  $this->renderLayout();

or to set a custom response

 //$file = myfile or html

 $this->getResponse()->setBody($file);
 $this->renderLayout();

Ideally your template should sit in /app/design/frontend/mypackage/mytheme/template/mytemplate.phtml



回答2:

You should read Magento doc and Alan Storm Blog. Alan also wrote a book about Magento Layout: No Frills Magento Layout.

Your url: http://domain.tld/modulename/controller/action/

The modulename_controller_action Handle is created by combining the route name (modulename), Action Controller name (controller), and Action Controller Action Method (action) into a single string. This means each possible method on an Action Controller has a Handle associated with it. In your layout xml handles this request:

<modulename_controller_action>
     ......
</modulename_controller_action>

Hope my suggestion is useful for you.