gpt4 book ai didi

magento - 在 Magento 中使用布局

转载 作者:行者123 更新时间:2023-12-01 12:41:52 24 4
gpt4 key购买 nike

正如我所见,每个模板文件都存在一个连接特定模块 block 的布局。我努力理解 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 的模板所做的是,它搜索布局而不是 block 文件,

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

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

在此布局定义中,type属性定义 block 文件,即,通过布局文件,模板获取 getCurrentCategory() 的值。来自 Block 的功能。

我们还有<reference name="content">, <reference name="left">它决定在何处附加模板。

我的问题是,

  1. 为什么不能 Templates直接从 Block 获取值不引用Layout ?

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

  3. 在考虑这 3 个 Block、Layout 和 Template 时,Layout 有什么用?

最佳答案

1 - 为什么 Templates 不能在不引用 Layout 的情况下直接从 Block 获取值?

他们可以。使用您的示例:

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

这可以写成:

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

并在实际 block 内 ( 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 个 block 、布局和模板时,布局的用途是什么?

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

假设您在 Magento 中创建了自己的模块,而布局并不存在 - 一切都在“ Controller ”中进行控制。您需要重写/扩展/破解核心 Magento 代码才能按照您想要的方式获得东西。如果您想在类别 View 页面上添加一个额外的小部件,您需要重写 Controller 方法,或添加您自己的 Controller 。

Magento Layout 通过创建一个全局布局文件克服了这个问题,您可以在不影响核心基础设施的情况下对其进行扩展。

让我们举个例子。

在类别 View 页面上,如果我们想更改上面的模板,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>

重申一下:

Also we have <reference name="content">, <reference name="left"> which decides where to append the template.

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

<!--
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>

进一步阅读:Introducing The Magento Layout

关于magento - 在 Magento 中使用布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23649302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com