2012-02-17 102 views
8

我是Magento的新成员,我正在尝试为我构建的模块进行布局。我有一个简单的模块和一个输出'Hello World'的IndexController(我已经使用了this教程)。Magento模块的布局

现在我想为该模块做一个布局,我已经使用了这个tutorial但它不起作用。有人可以指点我的教程,或者可以向我解释Magento中的布局是如何工作的吗?

THX :)

这是我到目前为止已经完成: 我有一个名为“安德烈”和模块的“Hello World”包。

这里是我的模块的config.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<config>  
    <modules> 
     <Andrei_Helloworld> 
      <version>0.1.0</version> 
     </Andrei_Helloworld> 
    </modules> 
    <frontend> 
     <routers> 
      <helloworld> 
       <use>standard</use> 
       <args> 
        <module>Andrei_Helloworld</module> 
        <frontName>helloworld</frontName> 
       </args> 
      </helloworld> 
     </routers> 
    </frontend> 
</config> 

这里是Andrei_Helloworld模块:

<?xml version="1.0" encoding="UTF-8"?> 
<config> 
    <modules> 
     <Andrei_Helloworld> 
      <active>true</active> 
      <codePool>local</codePool> 
     </Andrei_Helloworld> 
    </modules> 
</config> 

这里是我的控制器:

class Andrei_Helloworld_IndexController extends Mage_Core_Controller_Front_Action 
{ 
    public function indexAction() 
    { 
     echo 'Hello world'; 
    } 
} 

这是所有我已经完成了。该模块正常工作。我想为我的IndexController布局。 THX :)

+0

您应该向我们展示您的代码,主要是config.xml,xml布局文件和块php类。 – OSdave 2012-02-17 07:59:18

+0

我用我的代码编辑了我的问题:) – zuzuleinen 2012-02-17 08:08:06

回答

21

所以,有一些东西丢失...

  • 声明在你的config.xml布局更新:

    <frontend> 
        ... 
        <layout> 
         <updates> 
          <helloworld> 
           <file>helloworld.xml</file> 
          </helloworld> 
         </updates> 
        </layout> 
        ... 
    </frontend> 
    
  • 创建布局xml文件中, app/design/frontend/base/default/layout/helloworld.xml并在其中创建对模块/控制器/操作的引用:

    <?xml version="1.0"?> 
    <layout> 
        <helloworld_index_index> 
         <reference name="content"> 
          <block type="core/template" name="helloworld" template="helloworld.phtml" /> 
         </reference> 
        </helloworld_index_index> 
    </layout> 
    
  • 创建你刚才设置为你的XML布局文件模板的PHTML文件,即应用程序/设计/前端/基/默认/模板/ helloworld.phtml

    this is the content of helloworld.phtml 
    
  • 负荷使所有这些东西,在你的控制器的行动,将其替换您的echo声明:

    public function indexAction() 
    { 
        $this->loadLayout(); 
        $this->renderLayout(); 
    } 
    
  • 禁用缓存,刷新浏览器,坐享其成