草庐IT

php - 尝试在 magento 中创建自定义 block

coder 2024-04-07 原文

我是 stackoverflow 的新手,但我会很坦诚地提出我的疑问和问题。

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

我试过以下方法:

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

  2. 我在Magentotutorial里面做了一个基本的结构,就是五个目录。所以 位置是 magento/app/code/local/Magentotutorial/World

目录是:

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

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. 在我的 Block 目录中,它位于 magento/app/code/local/Magentotutorial/World/Block 我创建了一个名为 example.php 的文件

example.php 具有以下代码:

<?php
class Magentotutorial_World_Block_Example extends Mage_Core_Block_Template
{

}

5.现在我的 magento 目录中有布局和模板,位于 magento/app/design/frontend/rwd/default/layout 在我的布局文件中,我制作了具有以下代码的 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

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>
    

在这里我放出了我的代码

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

在这个文件中我使用了 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>

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

我的代码出了什么问题??我还想知道我的 block 类型 ="magentotutorial_world"是对的吗??

如果我想还原我的 test.xml 中的更改并想在我的 page.xml 文件中进行更改怎么办? 怎么做到的???

请帮帮我。

感谢和问候。

最佳答案

您在 magento/app/code/local/Magentotutorial/World/etc/config.xml 中输入了错误的 block 集。将 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>

关于php - 尝试在 magento 中创建自定义 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25743609/

有关php - 尝试在 magento 中创建自定义 block的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  3. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  4. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  5. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  6. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  7. ruby-on-rails - Enumerator.new 如何处理已通过的 block ? - 2

    我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m

  8. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  9. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  10. ruby-on-rails - Rails - 从另一个模型中创建一个模型的实例 - 2

    我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案

随机推荐