Вот мой помощник вида элемента привязки для zend. Вам нужно использовать мой помощник вида элемента изображения или удалить часть кода, которая его использует, если вам это не нравится. Конечно, вы можете изменить имя и все, что пожелаете.
require_once 'Zend/View/Helper/HtmlElement.php';
class Ecoweb_View_Helper_AnchorElement extends Zend_View_Helper_HtmlElement {
/**
*
* @param string $url
* @param string $content
* @param array|string $attribs
* @return string
*/
public function anchorElement($url, $content = '', $attribs = null)
{
if (is_array($url)) {
$reset = isset($url[2]) ? $url[2] : false;
$encode = isset($url[3]) ? $url[3] : false;
$url = $this->view->url($url[0], $url[1], $reset, $encode);
} else {
$url = $this->view->baseUrl($url);
}
if (is_array($attribs)) {
$attribs = $this->_htmlAttribs($attribs);
} else {
$attribs = empty($attribs) ? '' : ' '.$attribs;
}
if (is_array($content) && isset($content['src'])) {
$src = $content['src'];
$alt = isset($content['alt']) ? $content['alt'] : null;
$imgAttribs = isset($content['attribs']) ? $content['attribs'] : array();
$content = $this->view->imgElement($src, $alt, $imgAttribs);
}
$content = empty($content) ? $url : $this->view->escape($content);
$xhtml = '<a '
. 'href="'.$url.'"'
. $attribs
. '>'
. $content
. '</a>';
return $xhtml;
}
}
Вот помощник вида элемента изображения:
<?php
require_once 'Zend/View/Helper/HtmlElement.php';
class Ecoweb_View_Helper_ImgElement extends Zend_View_Helper_HtmlElement {
/**
*
* @param string $src
* @param string $alt
* @param array|string $attribs
* @return string
*/
public function imgElement($src, $alt = '', $attribs = null)
{
$src = $this->view->baseUrl($src);
if (is_array($attribs)) {
$attribs = $this->_htmlAttribs($attribs);
} else {
$attribs = empty($attribs) ? '' : ' '.$attribs;
}
$alt = $this->view->escape($alt);
$xhtml = '<img '
. 'src="'.$src.'" '
. 'alt="'.$alt.'"'
. $attribs
. $this->getClosingBracket();
return $xhtml;
}
}
Варианты использования:
echo $this->anchor('/mycontroller/myaction');
// output: <a href="/mycontroller/myaction">/mycontroller/myaction</a>
echo $this->anchor('/mycontroller/myaction', 'My anchor content', 'rel="nofollow"');
// output: <a href="/mycontroller/myaction" rel="nofollow">My anchor content</a>
echo $this->anchor('/mycontroller/myaction', 'My anchor content', 'rel="nofollow"');
// output: <a href="http://mydomain.com/mycontroller/myaction" rel="nofollow">My anchor content</a>
// when baseUrl is http://mydomain.com
echo $this->anchor(array(array('controller' => 'mycontroller', 'action' => 'myaction'), 'myroute'), 'My anchor content', array('rel' => 'nofollow'));
// output: <a href="/mycontroller/myaction" rel="nofollow">My anchor content</a>
echo $this->anchor('/mycontroller/myaction', array('src' => '/uploads/myimag.png'));
// output: <a href="/mycontroller/myaction"><img src="/uploads/myimag.png" alt=""></a>
// when you have an html doctype
echo $this->anchor('/mycontroller/myaction', array('src' => '/uploads/myimag.png', 'alt'=>'My alt text', array('width' => '100')));
// output: <a href="/mycontroller/myaction"><img src="/uploads/myimag.png" alt="My alt text" width="100" /></a>
// when you have an xhtml doctype