首页 文章

在Magento中使用布局

提问于
浏览
3

正如我在每个模板文件中看到的那样,存在一个连接特定模块块的布局 . 我努力了解Magento的每件作品,让我解释一下我做了什么,

考虑一个模板 app\design\frontend\base\default\template\catalog\category\view.phtml

我们有, $_category = $this->getCurrentCategory();

此功能属于Block app\code\core\Mage\Catalog\Block\Category\view.php

Magento模板的作用是,它搜索Layout而不是Block文件,

即内部布局文件, app\design\frontend\base\default\layout\catalog.xml

我们有, <block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml">

在此布局定义中, type 属性定义块文件,即,通过布局文件,模板从块获取 getCurrentCategory() 函数的值 .

我们还有 <reference name="content">, <reference name="left"> ,它决定了附加模板的位置 .

我的问题是,

  • 为什么 Templates 不能直接从 Block 获取值而不引用 Layout

  • 为什么Magento不允许我们这样做?

  • 在考虑这3块,布局和模板时,布局的用途是什么?

2 回答

  • 1

    1 - 为什么模板不能直接从Block获取值而不引用布局?

    他们能 . 使用你的例子:

    <block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml">
    

    这可以写成:

    <block type="catalog/category_view" name="category.products">
    

    并在实际块( app/code/core/Mage/Catalog/Block/Category/View.php )内:

    ...
    public function _construct()
    {
        parent::_construct();
        // set the template here instead
        $this->setTemplate('catalog/category/view.phtml');
    }
    ...
    

    2 - 为什么Magento不允许我们这样做?

    Magento允许你这样做 . Magento布局系统非常强大,虽然最初很难理解 .

    3 - 在考虑这些3块,布局和模板时,布局的用途是什么?

    我会用这个问题来澄清你的一些误解 . 如上所述,Magento布局非常强大,并且具有很大的灵活性,但乍一看这并不明显 . 我会尽力解释 .

    想象一下,你在Magento中创建了自己的模块,并且布局不存在 - 一切都在“控制器”内控制 . 您需要重写/扩展/破解核心Magento代码以获得您想要的方式 . 如果您想在类别视图页面上添加额外的小部件,则需要覆盖控制器方法或添加自己的控制器 .

    Magento布局通过创建一个全局布局文件来克服这个问题,您可以扩展它而不会弄乱核心基础架构 .

    让我们举个例子 .

    在类别视图页面上,如果我们想要将上述模板 catalog/category/view.phtml 更改为其他内容,例如 my/category/view.phtml ,我们可以这样做:

    <!-- this is the controller handle -->
    <catalog_category_view>
        <!--
            by referencing, we are actually referring to a 
            block which has already been instantiated. the
            name is the unique name within the layout.
    
            generally all blocks should have a name and it
            must be unique. however there can also be
            anonymous blocks but thats out of scope of this
            answer
        -->
        <reference name="category.products">
            <!--
                the layout allows us to fire methods within
                the blocks. in this instance we are actually
                firing the setTemplate method on the block
                and providing a "template" argument.
    
                this is the same as calling:
    
                $this->setTemplate('my/category/view.phtml');
    
                within the block.
            -->
            <action method="setTemplate">
                <template>my/category/view.phtml</template>
            </action>
        </reference>
    </catalog_category_view>
    

    只是重申:

    我们还有<reference name =“content”>,<reference name =“left”>,它决定了附加模板的位置 .

    这是不正确的 . “reference”标签允许您引用已实例化的块 . 只是为了完整性 - 以下示例向您展示了如何引用另一个块并在其中放置一个块:

    <!--
        the default layout handle which is call on nearly all
        pages within magento
    -->
    <default>
        <!--
            we are not referencing the "left" block
    
            we could if we wanted reference "right",
            "content" or any other blocks however 
            these are the most common
        -->
        <reference name="left">
            <!--
                add a block
    
                in this example we are going to reference
                "core/template" which translates to:
    
                Mage_Core_Block_Template
    
                which exists at:
    
                app/code/core/Mage/Core/Block/Template.php
            -->
            <block type="core/template" name="my.unique.name.for.this.block" template="this/is/my/view.phtml" />
        </reference>
    </default>
    

    Further reading: Introducing The Magento Layout

  • 3

    要回答你的问题,你需要挖掘Magento的MVC方法,

    网页在逻辑上分为几个部分,例如 Headers ,正文,页脚等,使页面布局井井有条,易于调整 . Magento通过Layout XML文件提供了这种灵活性 . Magento处理这些布局文件并将它们渲染到网页中 .

    Layout 文件充当应用程序的详细说明,内容涉及如何构建页面,构建页面的内容以及每个构建块的行为 .

    Layout 文件按模块分开,每个模块都带有自己的布局文件 . 系统以这种方式构建,以便无需添加和删除模块,而不会影响系统中的其他模块 .

    在Magento中,Model,View,Controller的View组件直接引用系统模型来获取显示所需的信息 .

    View 已分为块和模板 . Blocks 是PHP对象,模板是"raw"包含HTML和PHP混合的PHP文件 . 每个块都绑定到一个模板文件 . 在一个phtml文件中,PHP的$ this关键字将包含对Template的Block对象的引用 .

    布局文件包含 <handlers> ,它们映射到MVC控制器,所以期待你的处理程序

    <adminhtml_example_index> 将在 adminhtml/example/index controller 页面中使用

    <reference name="content"> 表示这些块中的块或其他引用将在主题模板的内容块中可用 .

    Magento中的每个页面请求都会生成几个独特的句柄 . Magento的MVC模式实现的' View '分为两部分: Layout and Template . 模板表示HTML块,而布局定义块在网页上的位置 .

    在Magento的MVC中方法,控制器不负责为View (in Magento’s case, the view is Layout and Blocks) 设置变量 . ControllersModels 上设置值,然后从这些相同模型中读取块 .

    你的控制器的工作是做某些事情 Models ,然后告诉系统它的布局渲染时间 . 根据系统模型的状态,以某种方式显示HTML页面是你的 Layout/Blocks 工作 .

    在Magento,当 URL 被触发时,

    • It determines Controller and action

    • Action Method manipulates Models

    • Action loads layout and starts rendering

    • Template, read from Models, via Blocks

    • Block and child blocks render into HTML

相关问题