14.4. 視圖協助器(View Helper)
在你的視圖指令碼中,經常需要執行某些特定的複雜的函數,例如,格式化日期,產生表單對象,或者顯示action的連結等等。你可以使用協助器類來完成這些工作。
你可以用$this->helperName()來調用helper。這時Zend_View會載入 Zend_View_Helper_HelperName類,建立一個對象執行個體,並調用它的helperName()方法。對象的執行個體會在 Zend_View的執行個體內一直存在,並可以被$this->helperName()重複調用。
14.4.1. 基本的協助器
Zend_View內建了幾個helper類,都是用來產生組件的。每個都有自動過濾變數的功能。
formButton($name, $value, $attribs): 產生<input type="button" />
formCheckbox($name, $value, $attribs, $options): 產生<input type="checkbox" />,$options參數是一個數組,第一個值是“checked”,第二個值是"unchecked"(預設為“1”和"0")。如果$ value匹配"checked"值,則這個checkbox顯示為已選中。
formFile($name, $value, $attribs): 產生<input type="file" />
formHidden($name, $value, $attribs): 產生<input type="hidden" />
formPassword($name, $value, $attribs): 產生<input type="password" />
formRadio($name, $value, $attribs, $options): 產生一系列<input type="button" />,每個$options數組元素一個,key為radio的值,並且元素的值是radio的標籤。
formReset($name, $value, $attribs): 產生<input type="reset" />
formSelect($name, $value, $attribs, $options): 建立一個<select>...</select>標籤,其中的每個<option>對應於一個$option數組 元素。元素的key是option的值,元素的值是option的標籤。$value這個值的option預設為選中。
formSubmit($name, $value, $attribs): 產生<input type="submit" />
formText($name, $value, $attribs): 產生<input type="text" />
formTextarea($name, $value, $attribs): 產生<textarea>...</textarea>
使用非常簡單,下面是個例子。
<?php
// inside your view script, $this refers to the Zend_View instance.
//
// say that you have already assigned a series of select options under
// the name $countries as array('us' => 'United States', 'il' =>
// 'Israel', 'de' => 'Germany').
?>
<form action="action.php" method="post">
<p><label>Your Email:
<?php echo $this->formText('email', 'you@example.com', array('size' => 32)) ?>
</label></p>
<p><label>Your Country:
<?php echo $this->formSelect('country', 'us', null, $this->countries) ?>
</label></p>
<p><label>Would you like to opt in?
<?php echo $this->formCheckbox('opt_in', 'yes', null, array('yes', 'no') ?>
</label></p>
</form>
以上視圖指令碼會輸出這樣的結果:
<form action="action.php" method="post">
<p><label>Your Email:
<input type="text" name="email" value="you@example.com" size="32" />
</label></p>
<p><label>Your Country:
<select name="country">
<option value="us" selected="selected">United States</option>
<option value="il">Israel</option>
<option value="de">Germany</option>
</select>
</label></p>
<p><label>Would you like to opt in?
<input type="hidden" name="opt_in" value="no" />
<input type="checkbox" name="opt_in" value="yes" checked="checked" />
</label></p>
</form>
14.4.2. Helper 的路徑
就像視圖指令碼那樣,你的控制器也可以設定helper的路徑給Zend_View。預設地,Zend_View會到 “Zend/View/Helper/”下尋找helper類。你可以用setHelperPath() 和 addHelperPath()來定義自己的路徑。
<?php
$view = new Zend_View();
$view->setHelperPath('/path/to/more/helpers');
?>
你可以用addHelperPath()來增加helper的路徑, Zend_View將使用最近增加的路徑。這樣你可以使用自己的helper。
<?php
$view = new Zend_View();
$view->addHelperPath('/path/to/some/helpers');
$view->addHelperPath('/other/path/to/helpers');
// now when you call $this->helperName(), Zend_View will look first for
// "/other/path/to/helpers/HelperName.php", then for
// "/path/to/some/helpers/HelperName", and finally for
// "Zend/View/Helpers/HelperName.php".
?>
14.4.3. 編寫自訂的Helper類
編寫自訂的Helper類很容易,只要遵循以下幾個原則即可:
類名必須是 Zend_View_Helper_*,*是helper的名稱。例如,你在寫一個名為“specialPurpose”的類,類名將是Zend_View_Helper_SpecialPurpose(注意大小寫)
類必須有一個public的方法,方法名與helper名稱相同。這個方法將在你的模板調用"$this->specialPurpose()"時 執行。在我們的“specialPurpose”例子中,相應的方法聲明可以是“public function specialPurpose()”。
一般來說,Helper類不應該echo或print或有其它形式的輸出。它只需要傳回值就可以了。返回的值應當被過濾。
類檔案的命名應該是helper方法的名稱,比如在"specialPurpose"例子中,檔案要存為“SpecialPurpose.php”。
把helper類的檔案放在你的helper路徑下, Zend_View就會自動載入,執行個體化,持久化,並執行。
下面是一個例子:
<?php
class Zend_View_Helper_SpecialPurpose {
protected $_count = 0;
public function specialPurpose()
{
$this->_count++;
$output = "I have seen 'The Jerk' {$this->_count} time(s).";
return htmlspecialchars($output);
}
}
?>
在視圖代碼中,你可以調用SpecialPurpose協助器任意次。它只需要執行個體化一次,就會在Zend_View執行個體的生命週期內持久存在。
<?php
// remember, in a view script, $this refers to the Zend_View instance.
echo $this->specialPurpose();
echo $this->specialPurpose();
echo $this->specialPurpose();
?>
輸出是這樣的:
I have seen 'The Jerk' 1 time(s).
I have seen 'The Jerk' 2 time(s).
I have seen 'The Jerk' 3 time(s).