尝试在magento中创建自定义块

时间:2014-09-09 11:34:01

标签: php xml magento magento-1.9.1

我是stackoverflow的新手,但我会非常怀疑我怀疑和问题。

我正在尝试在magento中创建自定义块。我是magento的新手并且学习它成为magento的高级开发人员。

我尝试了以下事项:

  1. 我在magento / app / code / local / Magentotutorial中创建了一个目录Magentotutorial。

  2. 我在其中制作了一个基本结构,它是Magentotutorial中的五个目录。所以 位置是magento / app / code / local / Magentotutorial / World

  3. 目录是:

    magento/app/code/local/Magentotutorial/World/controllers,
    magento/app/code/local/Magentotutorial/World/sql,
    magento/app/code/local/Magentotutorial/World/Model,
    magento/app/code/local/Magentotutorial/World/Helper,
    magento/app/code/local/Magentotutorial/World/Block,
    magento/app/code/local/Magentotutorial/World/etc.
    
    1. 我的config.xml进入magento/app/code/local/Magentotutorial/World/etc/config.xml
    2. config.xml文件包含以下代码:

      <?xml version="1.0" encoding="UTF-8"?>
      <config>
          <modules>
              <Magentotutorial_World>
                  <version>0.1.0</version>
              </Magentotutorial_World>
          </modules>
          <global>
              <blocks>
                  <Magentotutorial_World>
                      <class>Magentotutorial_World_Block</class>
                  </Magentotutorial_World>
              </blocks>
          </global>
          <frontend>
              <layout>
                  <updates>
                      <Magentotutorial_World>
                          <file>test.xml</file>
                      </Magentotutorial_World>
                  </updates>
              </layout>
          </frontend>
      </config>
      

      要在magento中激活我的模块,我创建了xml文件。

      Magentotutorial_World.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <config>
          <modules>
              <Magentotutorial_World>
                  <active>true</active>
                  <codePool>local</codePool>
              </Magentotutorial_World>
          </modules>
      </config>
      
      1. magento/app/code/local/Magentotutorial/World/Block中的我的阻止目录中 我已经创建了一个名为example.php
      2. 的文件

        example.php具有以下代码:

        <?php
        class Magentotutorial_World_Block_Example extends Mage_Core_Block_Template
        {
        
        }
        

        5。现在我在magento/app/design/frontend/rwd/default/layout的magento目录中有布局和模板 在我的布局文件中,我创建了test.xml文件,其中包含以下代码:

        <?xml version="1.0" encoding="UTF-8"?>
        <layout>
            <default>
                    <block type="magentotutorial_world/example" name="newreferenceBlock" template="test/example.phtml" />
            </default>
        </layout>
        
        1. 我在测试文件夹中创建了一个example.phtml文件,位于magento / app / design / frontend / rwd / default / template / test / example.phtml
        2. example.phtml具有以下代码:

          <html>
          <body>
              <h1 style="background-color:yellow">Hello New Reference!</h1>
          </body>
          </html>
          
          1. 现在在page.xml中我已经放了这段代码

                

            <default translate="label" module="page">
                <label>All Pages</label>
            
            
            
                <block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">
            
                    <block type="magentotutorial_world/example" output="toHtml" name="newReferenceBlock" template="test/example.phtml" />
            
                    <block type="page/html_head" name="head" as="head">
                        <action method="addJs"><script>prototype/prototype.js</script></action>
                        <action method="addJs"><script>lib/ccard.js</script></action>
                        <action method="addJs"><script>prototype/validation.js</script></action>
                        <action method="addJs"><script>scriptaculous/builder.js</script></action>
                        <action method="addJs"><script>scriptaculous/effects.js</script></action>
                        <action method="addJs"><script>scriptaculous/dragdrop.js</script></action>
                        <action method="addJs"><script>scriptaculous/controls.js</script></action>
                        <action method="addJs"><script>scriptaculous/slider.js</script></action>
                        <action method="addJs"><script>varien/js.js</script></action>
                        <action method="addJs"><script>varien/form.js</script></action>
                        <action method="addJs"><script>mage/translate.js</script></action>
                        <action method="addJs"><script>mage/cookies.js</script></action>
            
          2. 在这里,我已经推出了我的代码

            <block type="magentotutorial_world/example" output="toHtml" name="newReferenceBlock" template="test/example.phtml" />
            
            1. 现在我想将此模板呈现给我的页面的任何布局。我在这里尝试了2columns-right.phtml文件。
            2. 在这个文件中我使用了getChildHtml。

              <div class="col-left sidebar"><?php echo $this->getChildHtml('newreferenceBlock'); ?></div>
              

              整个文件是

              <?php
              /**
               * Magento
               *
               * NOTICE OF LICENSE
               *
               * This source file is subject to the Academic Free License (AFL 3.0)
               * that is bundled with this package in the file LICENSE_AFL.txt.
               * It is also available through the world-wide-web at this URL:
               * http://opensource.org/licenses/afl-3.0.php
               * If you did not receive a copy of the license and are unable to
               * obtain it through the world-wide-web, please send an email
               * to license@magentocommerce.com so we can send you a copy immediately.
               *
               * DISCLAIMER
               *
               * Do not edit or add to this file if you wish to upgrade Magento to newer
               * versions in the future. If you wish to customize Magento for your
               * needs please refer to http://www.magentocommerce.com for more information.
               *
               * @category    design
               * @package     rwd_default
               * @copyright   Copyright (c) 2014 Magento Inc. (http://www.magentocommerce.com)
               * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
               */
              ?>
              <?php
              /**
               * Template for Mage_Page_Block_Html
               */
              ?>
              
              <!DOCTYPE html>
              
              <!--[if lt IE 7 ]> <html lang="en" id="top" class="no-js ie6"> <![endif]-->
              <!--[if IE 7 ]>    <html lang="en" id="top" class="no-js ie7"> <![endif]-->
              <!--[if IE 8 ]>    <html lang="en" id="top" class="no-js ie8"> <![endif]-->
              <!--[if IE 9 ]>    <html lang="en" id="top" class="no-js ie9"> <![endif]-->
              <!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" id="top" class="no-js"> <!--<![endif]-->
              
              <head>
              <?php echo $this->getChildHtml('head') ?>
              </head>
              <body<?php echo $this->getBodyClass()?' class="'.$this->getBodyClass().'"':'' ?>>
              <?php echo $this->getChildHtml('after_body_start') ?>
              <div class="wrapper">
                  <?php echo $this->getChildHtml('global_notices') ?>
                  <div class="page">
                      <?php echo $this->getChildHtml('header') ?>
                      <div class="main-container col2-left-layout">
                          <div class="main">
                              <?php echo $this->getChildHtml('breadcrumbs') ?>
                              <?php // This left_first block will display above the main content on smaller viewports ?>
                              <?php if ($_leftContent = $this->getChildHtml('left_first')): ?>
                              <div class="col-left sidebar col-left-first"><?php echo $_leftContent;   ?></div>
                              <?php endif; ?>
                              <div class="col-main">
                                  <?php echo $this->getChildHtml('global_messages') ?>
                                  <?php echo $this->getChildHtml('content') ?>
                              </div>
                              <div class="col-left sidebar"><?php echo $this->getChildHtml('left') ?></div>
                          </div>
              <div class="col-left sidebar"><?php echo $this->getChildHtml('newreferenceBlock') ?></div>
                      </div>
                      <?php echo $this->getChildHtml('footer_before') ?>
                      <?php echo $this->getChildHtml('footer') ?>
                      <?php echo $this->getChildHtml('global_cookie_notice') ?>
                      <?php echo $this->getChildHtml('before_body_end') ?>
                  </div>
              </div>
              <?php echo $this->getAbsoluteFooter() ?>
              </body>
              </html>
              

              现在我无法在任何页面上呈现此块。基本上我想在我的索引页面上添加这个模板。

              我的代码出了什么问题?我也想知道那是我的块类型=“magentotutorial_world”是对的吗?

              如果我想恢复test.xml中的更改并希望在page.xml文件中进行更改,该怎么办? 怎么做???

              请帮助我。

              谢谢和问候。

2 个答案:

答案 0 :(得分:2)

您在magento/app/code/local/Magentotutorial/World/etc/config.xml中输入了错误的区块。将Magentotutorial_World更改为小写magentotutorial_world

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Magentotutorial_World>
            <version>0.1.0</version>
        </Magentotutorial_World>
    </modules>
    <global>
        <blocks>
            <magentotutorial_world>
                <class>Magentotutorial_World_Block</class>
            </magentotutorial_world>
        </blocks>
    </global>
    <frontend>
        <layout>
            <updates>
                <Magentotutorial_World>
                    <file>test.xml</file>
                </Magentotutorial_World>
            </updates>
        </layout>
    </frontend>
</config>

答案 1 :(得分:0)

我认为你的块类型应该是

<block type="world/example" output="toHtml" name="newReferenceBlock" template="test/example.phtml" />

如果我错了,请纠正我。