将图标添加到Zend Framework应用程序的最佳实践是什么?
我想将图标添加到我的应用程序。我把这些图标放在了public_html/images/icons /中,我想要一个干燥的方式将它们放在我的视图脚本中。所以我最好不要重复..将图标添加到Zend Framework应用程序的最佳实践是什么?
<img src="<?php echo $this->baseUrl();?>/images/icons/plus-circle.png"</a>
..每个图标。 我更喜欢简单的对象+函数调用。 这是最佳做法吗?
我嫌疑人我应该使用视图助手,但我还没有完全理解这些。
谢谢!
我会为此使用View Helper。
class My_View_Helper_Icon extends Zend_View_Helper_Abstract
{
public function icon($icon)
{
// not sure if you can use $this->baseUrl(); inside view helper
$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
$xhtml = sprintf('<img src="%s/images/icons/%s"', $baseUrl, $icon);
return $xhtml;
}
}
内,您的看法
echo $this->icon('plus-circle.png');
我有有方法$this->app()->getFileUrl('favicon.ico')
视图助手。它将首先搜索主题的位置,然后搜索公共位置。我将它分配给我的视图脚本顶部的一个变量,全部完成。
的视图助手和前端控制器插件源可以在这里找到: http://github.com/balupton/balphp/tree/master/trunk/lib/Bal/
或者说直接的代码: http://github.com/balupton/balphp/blob/master/trunk/lib/Bal/Controller/Plugin/App/Abstract.php#L721
”首先搜索主题的位置,然后搜索公共位置。“这听起来非常耗费资源,你有没有测试过这个速度? – 2010-09-16 15:06:51
这是微不足道的;它几乎没有被使用(并且它在哪里,结果被缓存),性能问题只会有两个'file_exists'检查(一个用于主题,一个用于公共)。 – balupton 2010-09-16 16:38:54
使用@ ArneRie的回答是:
在视图/帮手/Icon.php我写了以下类:
class Zend_View_Helper_Icon extends Zend_View_Helper_Abstract
{
//$icon is the name of an icon without the ".png" at the end because all icons
//are .png
public function icon($icon)
{
$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
return sprintf('<img src="%s/images/icons/%s.png">', $baseUrl, $icon);
}
}
在以views/scripts /索引我的视图文件/ index.phtml我然后调用图标对象的方法,像这样:
<?php echo $this->icon('plus-circle');?>
这里是我的版本:
class My_View_Helper_Icon extends Zend_View_Helper_HtmlElement
{
/**
*
* @param string $src Icon source
* @param array $attribs HTML Atrtibutes and values
* @param string $tag HTML tag name
* @return string HTML
*/
public function icon($src, $attribs = array(), $tag = 'img')
{
$attribs['src'] = $this->view->baseUrl($src);
$html = '<' . $tag . $this->_htmlAttribs($attribs) . $this->getClosingBracket();
return $html;
}
}
回报“
user228395
2010-09-16 14:16:38
我已经在新的答案中修改了您的答案,以使其在我的应用程序中可以正常工作。就我所知,你的答案基本上是好的。 – 2010-09-16 15:05:25
这很好,但是你不能用这种方法向'img'标签添加参数。您应该改为“Zend_View_Helper_HtmlTag”的子类。 – takeshin 2010-09-16 18:24:16