2014-05-14 71 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