Magento:在CMS中调用自定义块 [英] Magento: call a custom block in CMS

查看:103
本文介绍了Magento:在CMS中调用自定义块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Magento 1.9.1创建自己的模块.在我的模块中,我试图像这样在CMS内容中调用一个块:

I’m trying to create my own module for Magento 1.9.1. In my module, I am trying to call a block in CMS content like this:

{{block type="core/template" template="myNamespace/myModulOutput.phtml"}}

myModulOutput.phtml模板包含来自我自己的控制器的集合:

The myModulOutput.phtml template contains a collection from my own controller:

class myNamespace_myModelname_Block extends Mage_Core_Block_Template
{
    public function getCollection()
    {
       // some code
        return $collection;
    }
}

该模块似乎处于活动状态,并通过以下配置显示在Magento后端:

The module seems to be active and shows up in Magento backend with this configuration:

<config>
    <modules>
        <myNamespace_myModulname>
            <version>0.1.0</version>
        </myNamespace_myModulname>
    </modules>
    <global>
        <blocks>
            <myNamespace_myModulname> 
                <class>myNamespace_myModulname_Block</class>
            </myNamespace_myModulname>
        </blocks>
    </global>
</config>

块类在文件app/code/local/myNamespace/myModulname/Blocks/Index.php中定义.

这是有效的配置吗?我在前端出现错误:Fatal error: Call to a member function getCollection() on a non-object.

Is this a valid configuration? I am getting an error on the frontend: Fatal error: Call to a member function getCollection() on a non-object.

编辑

通过@ b.enoit.be的解释,我尝试了以下设置...它正在运行;-)

By explanation from @b.enoit.be I tried the following setting ... it's run ;-)

app/etc/modules/Mynamespace_Mymodulname.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Mynamespace_Mymodulname>
            <active>true</active>
            <codePool>local</codePool>
            <depends/>
        </Mynamespace_Mymodulname>
    </modules>
</config>

应用程序/代码/本地/Mynamespace/Mymodulname/Block/Index.php:

    <?php
    class Mynamespace_Mymodulname_Block_Index extends Mage_Core_Block_Template
    {
        public function getTest()
        {
           // some code
            return "mymodul:test";
        }
    }
    ?>

应用程序/代码/本地/Mynamespace/Mymodulname/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Mynamespace_Mymodulname>
            <version>0.1.0</version>
        </Mynamespace_Mymodulname >
    </modules>
    <global>
        <blocks>
            <mynamespace_mymodulname> 
                <class>Mynamespace_Mymodulname_Block</class>
            </mynamespace_mymodulname >
        </blocks>
    </global>
</config>

CMS通话

{{block type="mynamespace_mymodulname/index" template="mynamespace/myoutput.phtml"}}

app/design/frontend/myTheme/default/mynamespace/myoutput.phtml:

<?php /** @var $this Mynamespace_Mymodulname_Block_Index */ ?>
<?php echo $this->getTest() ?>

非常感谢您如此详尽而有意义的解释:-)

Many thanks for so detailed and meaningful explanation :-)

推荐答案

如果这些myNamespace_myModulname确实是您的实际代码中包含的内容,请首先查看

If those myNamespace_myModulname are really what you have in your actual code, please first have a look at @fantasticrice answer

然后,如果我们以正确的命名约定(请参阅本文结尾处的注释)考虑您的代码,那么这不是有效的块类:

Then, if we consider your code with the right naming convention (see note in the end of this post), to start up with, this is not a valid block class :

class Mynamespace_Mymodulename_Block extends Mage_Core_Block_Template{ /* ... */ }

如果您像稍后所说的那样,文件位于

If, like you say it later on, your file is located in

app/code/local/Mynamespace/Mymodulename/Block/Index.php

则有效的块类为

class Mynamespace_Mymodulename_Block_Index extends Mage_Core_Block_Template{ /* ... */ }

因为Magento中的类名应始终遵循与文件路径完全相同的体系结构(控制器除外,但我们根本不会在这里使用我们提供的代码来谈论控制器)= >

Because class name in Magento should always follow the exact same architecture as your path to file (except for controllers, but we are not talking about controllers at all with the code you gave us here) =>

class Mynamespace_Mymodulename_Block_Index === Mynamespace/Mymodulename/Block/Index.php

(请参阅我如何用下划线替换下划线,并用.php扩展名将其后缀吗?).

(see how I just replaced underscore with slashes and postfixed this with a .php extension ?).

然后,您真正想要的是使用自己的模块块的视图mynamespace/mymoduleoutput.phtml.
为此,您必须为块指定正确的类型.

Then what you actually want is your your view mynamespace/mymoduleoutput.phtml to use your own module block.
For this, you have to specify the right type for your block.

类型由config.xml中定义的句柄的组合以及从块文件的路径定义的.

The type is defined by a combinaison of your handle defined in config.xml and from the path to your block file.

为模块定义config.xml时,必须注意某些部分是固定的" ,而某些部分是处理" .
这意味着Magento希望某些零件存在有限的可能性,例如frontend or adminhtml or globalblocks, models, helpers, ... there is more here和其他一些名称只是为您的模块起别名,并按照我们在Magento下的称呼或处理它.

When you define a config.xml for a module, you have to be aware that some part are "fixed" and some parts are "handles".
Meaning that some parts are expected by Magento to be in a limited number of possibilities e.g. frontend or adminhtml or global, blocks, models, helpers, ... there is more here and some are just name to alias your module and to call it or handle as we call that under Magento.

在config.xml中,您说<global>配置(意味着前端和后端(= admin)均表示)– 已修复 –您想添加到以下列表中Magento的现有<blocks>-已修复-模块的模块--句柄- <mynamespace_mymodulename> ,然后将其映射到所有类均以Mynamespace_Mymodulename_Block开头的文件.

Here you say in your config.xml that for the <global> config (means for both frontend and backend (= admin)) -- fixed -- you want to add to the list of existing <blocks> -- fixed -- of Magento the blocks of your module that would be referenced by the -- handle -- <mynamespace_mymodulename> which will then map to files in which the classes will all begin with Mynamespace_Mymodulename_Block.

这是您想要用于自己的模块块的类型的第一部分.
mynamespace_mymodulename对于Magento等同于Mynamespace_Mymodulename_Block

That is the first part of the type you want for your own module block.
mynamespace_mymodulename would be equivalent for Magento to Mynamespace_Mymodulename_Block

然后,您只需指明从Blocks的根文件夹到Magento的正确路径,这与您的文件夹/文件体系结构完全相同:因此,在您的情况下,只需index.
因此,如您现在所了解的,Magento将在文件Mynamespace/Mymodulename/Block/Index.php中查找类Mynamespace_Mymodulename_Block_Index(句柄+'_'+指定的块).
但是,如果文件位于app/code/local/Mynamespace/Mymodulename/Block/Path/To/File.php下,则将具有path_to_file =>类Mynamespace_Mymodulename_Block_Path_To_File,文件Mynamespace/Mymodulename/Block/Path/To/File.php

Then you just have to indicate the right path from the root folder of your Blocks to Magento, which will be the exact same as your folders/files architecture, once again : so in your case, just index.
So as you may now understand, Magento will lookup to a class Mynamespace_Mymodulename_Block_Index (handle + '_' + specified block) in the file Mynamespace/Mymodulename/Block/Index.php as seen earlier.
But if your file was under app/code/local/Mynamespace/Mymodulename/Block/Path/To/File.php you would have path_to_file => class Mynamespace_Mymodulename_Block_Path_To_File, file Mynamespace/Mymodulename/Block/Path/To/File.php

现在我们有了类型的第二部分,我们只需要用斜杠将它们组合起来即可: mynamespace_mymodulename/index

Now that we have the second part of our type we just have to assemble them with a slash : mynamespace_mymodulename/index

因此,您必须将cms中的呼叫更改为:

So you have to change your call in your cms to :

{{block type="mynamespace_mymodulename/index" template="mynamespace/mymoduleoutput.phtml"}}

并希望以此解决问题.

And hopefully, with this, it would work.

注意1/3:正如 @fantasticrice 所指出的,我还建议您遵循类的命名约定:

NOTE 1/3 : As pointed out by @fantasticrice, I would also suggest you to follow the naming convention of class:

Zend Framework对类命名约定进行了标准化,因此 类的名称直接映射到它们所在的目录 储存. (...)

Zend Framework standardizes on a class naming convention whereby the names of the classes directly map to the directories in which they are stored. (...)

类名只能包含字母数字字符.编号是 在类名中允许使用,但在大多数情况下不建议使用. 仅允许使用下划线代替路径分隔符;这 文件名"Zend/Db/Table.php"必须映射到类名 "Zend_Db_Table".

Class names may only contain alphanumeric characters. Numbers are permitted in class names but are discouraged in most cases. Underscores are only permitted in place of the path separator; the filename "Zend/Db/Table.php" must map to the class name "Zend_Db_Table".

如果班级名称包含多个单词,则第一个字母 每个新单词的大写必须大写.连续大写字母 不允许,例如课时不允许使用"Zend_PDF"类 "Zend_Pdf"是可以接受的.

If a class name is comprised of more than one word, the first letter of each new word must be capitalized. Successive capitalized letters are not allowed, e.g. a class "Zend_PDF" is not allowed while "Zend_Pdf" is acceptable.

这些约定为Zend定义了伪命名空间机制 框架. Zend Framework会在其采用PHP名称空间功能时使用 变得可用,并且对于我们的开发人员在他们的开发中使用是可行的 应用程序.

These conventions define a pseudo-namespace mechanism for Zend Framework. Zend Framework will adopt the PHP namespace feature when it becomes available and is feasible for our developers to use in their applications.

有关示例,请参见标准库和附加库中的类名 此类约定的名称.

See the class names in the standard and extras libraries for examples of this classname convention.

来源: http://framework.zend .com/manual/1.12/en/coding-standard.naming-conventions.html

注意2/3::再次按照惯例,只有类将具有大写的文件名/文件夹结构.因此,模板文件夹和文件中根本就没有大写字母.

NOTE 2/3 : that per convention again, only class are going to have a capitalised file name / folder structure. So, your template folders and files should not have capital in it at all.

注意3/3:同样,在Magento中,根据惯例,我们不在句柄中使用大写字母.

NOTE 3/3 : In Magento, again, per convention we do not use capitals in handles.

app/code/local/Mynamespace/Mymodulename/etc/config.xml

app/code/local/Mynamespace/Mymodulename/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mynamespace_Mymodulename>
            <version>0.1.0</version>
        </Mynamespace_Mymodulename>
    </modules>
    <global>
        <blocks>
            <mynamespace_mymodulename> 
                <class>Mynamespace_Mymodulename_Block</class>
            </mynamespace_mymodulename>
        </blocks>
    </global>
</config>

app/code/local/Mynamespace/Mymodulename/Block/Index.php

app/code/local/Mynamespace/Mymodulename/Block/Index.php

<?php
class Mynamespace_Mymodulename_Block_Index extends Mage_Core_Block_Template
{
    public function getCollection()
    {
       // some code
        return $collection;
    }
}

CMS内容

{{block type="mynamespace_mymodulename/index" template="mynamespace/mymoduleoutput.phtml"}}

app/etc/modules/Mynamespace_Mymodulename.xml

app/etc/modules/Mynamespace_Mymodulename.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mynamespace_Mymodulename>
            <active>true</active>
            <codePool>local</codePool>
            <depends/>
        </Mynamespace_Mymodulename>
    </modules>
</config>

app/design/frontend/base/default/template/mynamespace/mymoduleoutput.phtml

app/design/frontend/base/default/template/mynamespace/mymoduleoutput.phtml

<?php /** @var $this Mynamespace_Mymodulename_Block_Index */ ?>
<?php foreach($this->getCollection() as $item): ?>
    <?php //do something ?>
<?php endforeach; ?>

这篇关于Magento:在CMS中调用自定义块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆