跳轉到內容

Mojavi 3 手冊/教程/裝飾器分析

來自華夏公益教科書

先決條件

[編輯 | 編輯原始碼]

在閱讀本教程之前,應該先了解 ((全域性模板 - 裝飾器模式教程)(教程)). 在閱讀本教程之前,應該先了解 ((http://wiki.mojavi.org:81/159.html)(全域性 模板教程)).


++ 分析

++ 分析


裝飾器分析是一個簡單的概念:將裝飾器模板和填充器的不同排列分離到可以自動執行這些呼叫的指令碼中。裝飾器分析是一個簡單的概念:將裝飾器模板和填充器的不同排列分離到可以自動執行這些呼叫的指令碼中。首先,讓我們從演變的角度看看如何將不同的顯示模式或配置檔案與實際檢視分離。

++ 階段 1:硬編碼

在這個示例中,一個簡單直接裝飾模板的示例完成了它的工作。但它沒有提供一種簡單的方法來在其他檢視中複製這種裝飾器/插槽填充模式,而無需複製貼上。 // 1 ka ans kya h?

示例 1:硬編碼裝飾器資訊

[編輯 | 編輯原始碼]
class IndexSuccessView extends PHPView
{

public function execute ()
{

// set our template
$this->setTemplate('IndexSuccess.php');

//setup our decorator template
$this->setDecoratorDirectory(MO_TEMPLATE_DIR);
$this->setDecoratorTemplate('myGlobalTemplate.php');

//setup our slots
//(SlotName, Module, Action)
$this->setSlot('menu', 'Content', 'PopulateMenu');
$this->setSlot('css', 'Content', 'PopulateCss');

// set the title
$this->setAttribute('title', 'Default Action');

}

}

++ 階段 2:包含

抽象的第二個階段是建立一個單獨的指令碼(一個單獨的 php 檔案)並將其直接包含到檢視中,從而消除複製的程式碼。

示例 2:包含裝飾器資訊

[編輯 | 編輯原始碼]
class IndexSuccessView extends PHPView
{

public function execute ()
{

// set our template
$this->setTemplate('IndexSuccess.php');

// set the title
$this->setAttribute('title', 'Default Action');

//get our profile
require(MO_TEMPLATE_DIR . '/profiles/Default.profile.php');

}

}

正如您所見,所有裝飾器呼叫都已刪除,取而代之的是一個包含位置。包含檔案如下所示

示例配置檔案:Default.profile.php

[編輯 | 編輯原始碼]
<?php

//setup our decorator template
$this->setDecoratorDirectory(MO_TEMPLATE_DIR);
$this->setDecoratorTemplate('myGlobalTemplate.php');

//setup our slots
$this->setSlot('menu', 'Content', 'PopulateMenu');
$this->setSlot('css', 'Content', 'PopulateCss');

?>

++ 階段 3:=setProfile()=

最後一個階段,或者我建議的階段,是建立一個簡單的方法來為我們載入配置檔案,並可能提供一些其他實用程式函式,例如

hasProfile()
loadProfile() or setProfile()

示例 3:包含裝飾器資訊

[編輯 | 編輯原始碼]
class IndexSuccessView extends PHPView
{

public function execute ()
{

// set our template
$this->setTemplate('IndexSuccess.php');

// set the title
$this->setAttribute('title', 'Default Action');

//get our profile
$this->setProfile('Default');

}

}

++ =setProfile()= 方法

不要被欺騙,=setProfile()= 方法和包含方法最終是等效的。一種方法比另一種方法需要多呼叫一個函式,但我認為 =setProfile()= 的簡單性使其更勝一籌,此外它還提供自動配置檔案檢測,以及在適當目錄中的自動搜尋(我猜想儲存配置檔案的目錄可能是 MO_TEMPLATE_DIR/templates/profiles/)。

示例 4:setProfile() 方法

[編輯 | 編輯原始碼]
Note: This function is untested and may contain errors.


public function setProfile($name, $module = null)
{

//is the module null?
if($module === null)
{

//set the directory to the global profile directory
$dir = MO_TEMPLATE_DIR . '/profiles';

}
else
{

//set the directory to the module profile directory
$dir = MO_MODULE_DIR . '/' . $module . '/templates/profiles';

}

//setup the profile path
$profile = $dir . '/' . $name . '.profile.php';

//can we read it?
if(is_readable($profile))
{

//get the file
require($profile);

//set the profile up
$this->profile = $name;
return true;

}
else
{

//profile failed, set null
$this->profile = null;
return false;

}

}

成員變數 =$profile= 允許在檢視的 preRenderCheck() 期間執行檢查。

華夏公益教科書