2014-05-14 73 views
3

正如我所看到的每个模板文件都存在一个连接特定模块块的布局。我挣扎着理解Magento的每个块,让我解释我做了什么,在Magento中使用布局

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

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

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

什么Magento的模板它的搜索布局,而不是块文件,

即,内部布局文件,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">它决定在哪里追加模板。

我的问题是,

  1. 为什么不能TemplatesBlock直接获得价值,而不指Layout

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

  3. 在考虑这些3块,布局和模板时,布局有什么用处?

回答

1

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

一个网页被分成几个部分逻辑,例如,头,身,尾等,使得页面布局有组织且易于调整。 Magento通过Layout XML文件提供了这种灵活性。 Magento处理这些布局文件并将其渲染到网页中。

布局文件充当应用程序关于如何构建页面,构建它以及每个构建块的行为的详细说明。

布局文件在每个模块分开,每个模块带来与它自己的布局文件。系统建成这样,才能允许无缝添加和删除模块没有在系统中影响其他模块。

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

查看已分为块和模板。 是PHP对象,模板是包含HTML和PHP混合的“原始”PHP文件。每个块都绑定到一个单独的模板文件。在phtml文件中,PHP的$ this关键字将包含对模板的Block对象的引用。

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

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

<reference name="content">意味着那些块内的块或其他参考将在你的主题模板上的内容块中可用。

在Magento每个页面请求生成几个不同的手柄。 Magento的MVC模式实现的'视图'分为两部分:布局和模板。模板代表一个HTML块,而布局则定义了网页上块的位置。

在Magento的MVC方法,它不是控制器设置变量视图(in Magento’s case, the view is Layout and Blocks)的责任。 控制器上模式设定值,然后阻止来自这些相同的模型读取。

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

在Magento,当URL被触发,

  1. 它决定了控制器和行动
  2. 操作方法操纵模型
  3. 行动负载布局,并开始呈现
  4. 模板,从M中读取odels,通过块
  5. 块和子块渲染成HTML
+0

你的所有定义敲我明白究竟会在Magento设计区内。谢谢。 – Vinith

3

1 - 为什么不能从模板座获得价值没有直接提及布局?

他们可以。使用你的例子:

<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 Layout通过创建全局布局文件克服了这一点,您可以在不影响核心基础结构的情况下扩展该布局文件。

让我们来看看你的例子。

在一个类别视图页面上,如果我们想改变上面的模板,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">,它决定在哪里追加模板。

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

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

+0

我的答案是,1.模板无法直接从Block获取值,而无需引用布局。 2.不清楚。 3.清除布局就像是Magento Body的骨骼。 – Vinith