我需要通過Zend_Form的結果的總結我的文本渲染:我如何包裝由Zend_Form的結果一些文字渲染
您可以通過代碼都明白(因爲我的英語不好)(該樣本是不工作):
$HTML = $Form -> render(new Zend_View('asdasd'));
我應該得到:
<form>
asdasd
</form>
我需要通過Zend_Form的結果的總結我的文本渲染:我如何包裝由Zend_Form的結果一些文字渲染
您可以通過代碼都明白(因爲我的英語不好)(該樣本是不工作):
$HTML = $Form -> render(new Zend_View('asdasd'));
我應該得到:
<form>
asdasd
</form>
您可以輕鬆地渲染來自控制器的形式,這樣
在控制器
$form = new Zend_Form();
//Create your form elements
$this->view->form = $form
然後在控制器
echo $this->form
Zend_Form
的對象有一個__toString()
方法將通過簡單地使用回波呈現的形式爲您服務。
我認爲你正在嘗試做的事情(如果我正確理解你的話)可以通過編寫自定義窗體視圖助手來實現。出於這個原因,我準備了一個如何完成的草案。不確定,如果這是做這件事的最好方式,但它應該起作用。
首先,讓我們創建我們的自定義表單視圖幫助器。默認的是Zend_View_Helper_Form
。 Zend_Form使用Zend_Form_Decorator_Form
進行渲染,然後使用Zend_View_Helper_Form
實際生成表單的xhtml表示形式。所以我們可以創建自己的表單助手,例如叫My_View_Helper_CustomForm
延伸Zend_View_Helper_Form
如下:
// Example file location: APPLICATION_PATH/views/helpers/CustomForm.php
class My_View_Helper_CustomForm extends Zend_View_Helper_Form {
/**
* Render HTML form
*
* @param string $name Form name
* @param null|array $attribs HTML form attributes
* @param false|string $content Form content
* @return string
*/
public function customForm($name, $attribs = null, $content = false)
{
// THE FOLLOWING FEW LINES ARE NEW.
// get myText and unset it from $attribs afterwards.
$myText = '';
if (array_key_exists('myText', $attribs)) {
$myText = $attribs['myText'];
unset($attribs['myText']);
}
// this is from orginal form view helper
$info = $this->_getInfo($name, $content, $attribs);
extract($info);
if (!empty($id)) {
$id = ' id="' . $this->view->escape($id) . '"';
} else {
$id = '';
}
if (array_key_exists('id', $attribs) && empty($attribs['id'])) {
unset($attribs['id']);
}
$xhtml = '<form'
. $id
. $this->_htmlAttribs($attribs)
. '>';
// THE NEXT LINE IS AGAIN NEW
// WE PUT $myText after opening <form> tag (as in your example).
$xhtml .= $myText . 'sadfsf';
// the rest of the form, as usuall.
if (false !== $content) {
$xhtml .= $content
. '</form>';
}
return $xhtml;
}
}
某處在Bootsrap.php你應該在視圖助手本地化添加到自動加載。也許你也可以通過將視圖助手目錄添加到Zend_View來實現。不過,我只是做使用自動加載它,如下所示:
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
));
$resourceLoader->addResourceType('view', 'views/helpers/', 'My_View_Helper_');
$autoLoader->pushAutoloader($resourceLoader);
已經這樣做了,我會證明testAction示例代碼將使用上述customForm:
$myForm = new My_Form_SomeForm();
// set your text you want to put in the form
// Just don't forget to unset it the new view helper.
$myForm->setAttrib('myText', 'asdasd');
// get form decorator (i.e. Zend_Form_Decorator_Form)
$formDecorator = $myForm->getDecorator('form');
// $formDecorator uses Form helper to actually cunstruct xhtml of the form.
// So we set our new helper (i.e. My_View_Helper_CustomForm)
// instead of default one (i.e. Zend_View_Helper_Form).
// setHelper() method takes only string, thus ZF will need to know how to find
// the new view helper. This was the reason I added it to the autoloader.
$formDecorator->setHelper('customForm');
$HTML = $myForm->render();
希望這將是有用你會幫助你解決問題。
我不確定你想要做什麼。 Zend_View需要一個$ config數組,而不是一個字符串。你能否試圖澄清你想要做什麼。 – Marcin 2011-02-03 11:41:47