代码之家  ›  专栏  ›  技术社区  ›  Niels Bom

向Zend框架应用程序添加图标的最佳实践是什么?

  •  0
  • Niels Bom  · 技术社区  · 15 年前

    我想给我的应用程序添加图标。我已经把图标公开了,我想用一种干燥的方式把它们放到我的视图脚本中。所以我最好不要重复……

    <img src="<?php echo $this->baseUrl();?>/images/icons/plus-circle.png"</a>
    

    …每个图标。 我更喜欢简单的对象+函数调用。 最佳实践是什么?

    犯罪嫌疑人 我应该使用视图助手,但我还不完全理解这些。

    谢谢!

    4 回复  |  直到 15 年前
        1
  •  2
  •   opHASnoNAME    15 年前

    我将为此使用视图助手。

    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');
    
        2
  •  0
  •   balupton    15 年前

    我有一个视图助手,它有一个方法 $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

        3
  •  0
  •   Niels Bom    15 年前

    使用@arnerie的答案:

    在views/helpers/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);
        }
    }
    

    在视图/scripts/index/index.phtml中的视图文件中,然后调用icon对象的方法,如下所示:

    <?php echo $this->icon('plus-circle');?>
    
        4
  •  0
  •   takeshin    15 年前

    这是我的版本:

    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;
        }
    }