我正在爲自己的項目編寫基本模板類。基本用法是這樣的:如何模擬模板類
$template = new Template('template_file.php');
$template->assignVariable('pageTitle', 'Home page');
$template->render();
'template_file.php' 的內容:
<?php print $pageTitle; ?>
這一步什麼模板類確實步:在私人陣列
- 房屋變量當調用assignVariable方法時
- 當調用render方法時,提取存儲的變量,包含模板文件
ob_start()
和ob_end_clean()
塊。將輸出存儲在變量ob_get_contents()
中,然後打印存儲的輸出。
我知道這是一個非常簡單的模板類,但按預期工作。問題是我應該將包含模板文件委託給另一個類?我在爲這堂課寫單元測試時遇到了這個問題。我認爲應該封裝文件系統交互。你怎麼看?如果你認爲它不應該,我怎麼可以在我的測試中包含一個文件?
也許我只是通過模板文件的內容到類像這樣:
$templateContent = file_get_contents('template_file.php');
$template = new Template($templateContent);
...
編輯:我決定來封裝模板類的輸入過程,寫出更好的單元測試的緣故和封裝。但正如johannes指出的那樣,我需要使用eval()
來達到這個目的,這看起來並不正確。 Johannes向我指出了在單元測試中嘲笑包含的流式封裝的方向。但是這給我一個新想法。這是我要做的事情;我將繼續在我的模板類中使用include()
,但這次使用流封裝。初始化時,我會將協議處理程序傳遞給我的模板類。通過這種方式,我可以創建自己的流封裝以從數據庫獲取模板數據或使用局部變量。以下是示例:
$template = new Template('file://template_file.php');
stream_wrapper_register('database', 'My_Database_Stream');
$template = new Template('database://templates/3'); // templates table, row id 3
stream_wrapper_register('var', 'My_Var_Stream');
$myTemplate = '<?php print "Hello world!"; ?>';
$template = new Template('var://myTemplate');
我已經爲本地變量實現了自定義流包裝。那就是:
class My_Var
{
protected $position;
protected $variable;
function stream_open($path, $mode, $options, &$openedPath) {
$url = parse_url($path);
global $$url['host'];
$this->variable = $$url['host'];
$this->position = 0;
return true;
}
public function stream_read($count) {
$ret = substr($this->variable, $this->position, $count);
$this->position = strlen($ret);
return $ret;
}
public function stream_eof() {
return $this->position >= strlen($this->variable);
}
}
stream_wrapper_register('var', 'My_Var');
$myvar = '<?php print "mert"; ?>';
include 'var://myvar';
exit;
是的,但是當使用include函數時,我需要一個虛擬模板文件在我的測試文件夾中。我也不應該只測試模板類嗎?另外我將如何模擬包含功能? – matte 2010-01-17 13:19:01
您可以使用自定義流包裝器或數據提供程序來發送模擬數據。 http://php.net/stream_wrapper_register http://php.net/manual/en/wrappers.data.php – johannes 2010-01-17 13:44:36
嗨Johannes,謝謝!你爲我啓發了一個新想法。我編輯了這個問題,並添加了我提出的解決方案。 – matte 2010-01-17 18:40:47