あなたのファイルの要素へのリンクを追加するViewHelper Decoratorと一緒にView Helperを使用する簡単な方法です。
まず、あなたはすでに、セットアップヘルパーパスを持っているapplication.ini
にこれを追加しない場合:
resources.view.helperPath.My_View_Helper = "My/View/Helper/"
次に、あなたのパスに(library
フォルダが素晴らしい作品)、ディレクトリツリーMy/View/Helper
を作成します。
私はそれLink
を呼び出しています例えば、上記のディレクトリにビューヘルパーを作成し、そのLink.phpのMy/View/Helper/Link.php
内容を作成することです:
<?php
class My_View_Helper_Link extends Zend_View_Helper_Abstract
{
public function link($name, $value, $attribs, $elOptions)
{
if (!isset($attribs['linkOpts']) || !is_array($attribs['linkOpts']))
return '';
$linkOpts = $attribs['linkOpts'];
$link = (isset($linkOpts['href'])) ? $linkOpts['href'] : '';
$text = (isset($linkOpts['text'])) ? $linkOpts['text'] : '';
if ($link == '' || $text == '') return '';
return sprintf('<a href="%s">%s</a>', $link, htmlspecialchars($text));
}
}
さて、あなたが作成したときに、あなたの要素を追加するには、ViewHelper
デコレータを追加し、いくつかのリンクオプションを渡すだけです。あなたはファイルの要素に対するデコレータのスタックを使用して、要素にlinkOpts
を提供する場合
$fileDecorators = array(
'File',
array('ViewHelper', array('helper' => 'link')), // Add ViewHelper decorator telling it to use our Link helper
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('class' => 'form-div')),
array('Label', array('class' => 'form-label', 'requiredSuffix' => '*'))
);
$this->addElement('file', 'file1', array(
'label' => 'File Upload:',
'required' => false,
'decorators' => $fileDecorators,
'validators' => array(
/* validators here */
),
'linkOpts' => array('href' => 'http://site.com/page/link',
'text' => 'This is the link text',
),
));
さて、それはファイルの入力後にリンクをレンダリングします。 linkOpts
が指定されていない場合、またはhref
またはtext
要素の場合、File要素の後にはリンクが出力されません。
希望に役立ちます。
いい例、ありがとう –