Welcome Guest, Not a member yet? Register   Sign In
NONSENSE in html helper...
#1

[eluser]walrus_lt[/eluser]
Hi. I found nonsense in ci/html_helper and i hate it because it wastes cpu... Just look at this...:

Code:
if ( ! function_exists('img'))
{
    function img($src = '', $index_page = FALSE)
    {
        if ( ! is_array($src) )
        {
            $src = array('src' => $src);
        }

        $img = '<img';

        foreach ($src as $k=>$v)
        {

            if ($k == 'src' AND strpos($v, '://') === FALSE)
            {
                $CI =& get_instance();

                if ($index_page === TRUE)
                {
                    $img .= ' src="'.$CI->config->site_url($v).'" ';
                }
                else
                {
                    $img .= ' src="'.$CI->config->slash_item('base_url').$v.'" ';
                }
            }
            else
            {
                $img .= " $k=\"$v\" ";
            }
        }

        $img .= '/>';

        return $img;
    }
}

Now look closer:

Code:
foreach ($src as $k=>$v)
        {

            if ($k == 'src' AND strpos($v, '://') === FALSE)
            {
                <...>

                $CI =& get_instance();

                <...>

$CI =& get_instance(); in loop? Wtf..
#2

[eluser]wiredesignz[/eluser]
Post some feedback in the bug forum and maybe the Ellislab geeks will overhaul the code.

Although I have to admit I do see worse code posted these forums quite regularly. Tongue
#3

[eluser]Pascal Kriete[/eluser]
It only runs for the src attribute. In other words - once per function call (unless your image tag has two src's...). In fact, not even on every function call, only if the src attribute is not a full url.

I'm having a hard time optimizing it further, but I'm willing to consider alternatives.
#4

[eluser]wiredesignz[/eluser]
Using the img function in the html helper would generally suggest people will be using a relative url rather than a full url. (local assets et al)

But you're right it's pretty trivial. Even if it is really terrible code.




Theme © iAndrew 2016 - Forum software by © MyBB