How to create new page in magento site

2020-04-10 03:09发布

问题:

I am trying to create a new page in magento ,in which I have to add some html and javascript. For this ,I have created a module . Contents of -> app\code\local\CompanyName\HelloWorld\etc\config.xml -

 <?xml version="1.0"?>
 <config>
<modules>
    <CompanyName_Helloworld>
        <version>
            0.1.0
        </version>
    </CompanyName_Helloworld>
</modules>
<frontend>
    <routers>
        <helloworld>
            <use>standard</use>
            <args>
                <module>CompanyName_Helloworld</module>
                <frontName>Helloworld</frontName>
            </args>
        </helloworld>
    </routers>

</frontend>

Contents of -> app\code\local\CompanyName\HelloWorld\controllers\IndexController.php -

<?php
 class CompanyName_Helloworld_IndexController extends   Mage_Core_Controller_Front_Action{
public function indexAction(){
    $this->loadLayout();
           $this->renderLayout();
    //echo "We're echoing just to show that this is what's called, normally you'd have some kind of redirect going on here";
  }
 }

?>

After doing all this when I goto domain/index.php/Helloworld I can see the header and the footer,Now I want to add some "div" tags and javascript between them. Please Explain how to do it.

回答1:

Insert to module's config.xml:

<frontend>
...
  <layout>
    <updates>
      <helloworld>
        <file>helloworld.xml</file>
      </helloworld>
    </updates>
  </layout>
</frontend>

Next add layout file app/design/frontend/default/default/layout/helloworld.xml:

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
  <helloworld_index_index>
    <reference name="content">
      <block type="helloworld/index" name="helloworld_any_block" template="helloworld/index.phtml" />
    </reference>
  </helloworld_index_index>
</layout>

Eventually add phtml file app/design/frontend/default/default/template/helloworld/index.phtml with any content.