Welcome Guest, Not a member yet? Register   Sign In
HTML Helper Addition - script_tag()
#1

[eluser]OregonGold[/eluser]
I noticed there was a link_tag() helper but not a script_tag() helper. So i decided to add it in. If you want it, enjoy.

PLEASE NOTE!!!

the forum is removing the script tags from the code below, So remove the forward slash from all instances of the word script "s/cript"


Code:
// ------------------------------------------------------------------------

/**Added by Robert Blanchard
*
* Script
*
* Generates script tag to JS file
*
* @access    public
* @param    mixed    js src
* @param    string    type
* @param    boolean    should index_page be added to the css path
* @return    string
*/
if ( ! function_exists('script_tag'))
{
    function script_tag($src = '', $type = 'text/javascript', $index_page = FALSE)
    {
        $CI =& get_instance();

        $script = '<s/cript';
            {
                if ($k == 'src' AND strpos($v, '://') === FALSE)
                {
                    if ($index_page === TRUE)
                    {
                        $script .= ' src="'.$CI->config->site_url($v).'" ';
                    }
                    else
                    {
                        $script .= ' src="'.$CI->config->slash_item('base_url').$v.'" ';
                    }
                }
                else
                {
                    $script .= "$k=\"$v\" ";
                }
            }

            $script .= "></s/cript>";
        }
        else
        {
            if ( strpos($src, '://') !== FALSE)
            {
                $script .= ' src="'.$src.'" ';
            }
            elseif ($index_page === TRUE)
            {
                $script .= ' src="'.$CI->config->site_url($src).'" ';
            }
            else
            {
                $script .= ' src="'.$CI->config->slash_item('base_url').$src.'" ';
            }

            $script .= 'type="'.$type.'"';

            $script .= '></s/cript> ';
        }


        return $script;
    }
}

usage

Code:
echo script_tag('javascript/jquery.js');

produces
Code:
<s/cript src="http://www.domain.com/javascript/jquery.js" type="text/javascript"></s/cript>
#2

[eluser]Medikal[/eluser]
Nice job man, looks good. I'm always trying to think of ideas to make, can never think of any Tongue
#3

[eluser]Unknown[/eluser]
html code is to easy but main thing is create the page. and done anything.

Photo to Canvas
#4

[eluser]OregonGold[/eluser]
[quote author="semon" date="1291224434"]html code is to easy but main thing is create the page. and done anything.

Photo to Canvas[/quote]

I have no clue what you just said. This is a helper, they have a helper for everything else so why just just add this to the list. Its not needed just like link_tag or img() but its there.
#5

[eluser]Dennis Rasmussen[/eluser]
[quote author="OregonGold" date="1291225575"][quote author="semon" date="1291224434"]html code is to easy but main thing is create the page. and done anything.

url removed[/quote]

I have no clue what you just said. This is a helper, they have a helper for everything else so why just just add this to the list. Its not needed just like link_tag or img() but its there.[/quote]

Hi OregonGold, say hello to the forum spam Smile
(it's a spam reply)
#6

[eluser]OregonGold[/eluser]
Ah go figure, sheesh. cant escape it.
#7

[eluser]Unknown[/eluser]
[quote author="OregonGold" date="1291197101"]
Code:
// ------------------------------------------------------------------------

/**Added by Robert Blanchard
*
* Script
*
* Generates script tag to JS file
*
* @access    public
* @param    mixed    js src
* @param    string    type
* @param    boolean    should index_page be added to the css path
* @return    string
*/
if ( ! function_exists('script_tag'))
{
    function script_tag($src = '', $type = 'text/javascript', $index_page = FALSE)
    {
        $CI =& get_instance();

        $script = '<s/cript';
            {
                if ($k == 'src' AND strpos($v, '://') === FALSE)
                {
                    if ($index_page === TRUE)
                    {
                        $script .= ' src="'.$CI->config->site_url($v).'" ';
                    }
                    else
                    {
                        $script .= ' src="'.$CI->config->slash_item('base_url').$v.'" ';
                    }
                }
                else
                {
                    $script .= "$k=\"$v\" ";
                }
            }

            $script .= "></s/cript>";
        }
        else
        {
            if ( strpos($src, '://') !== FALSE)
            {
                $script .= ' src="'.$src.'" ';
            }
            elseif ($index_page === TRUE)
            {
                $script .= ' src="'.$CI->config->site_url($src).'" ';
            }
            else
            {
                $script .= ' src="'.$CI->config->slash_item('base_url').$src.'" ';
            }

            $script .= 'type="'.$type.'"';

            $script .= '></s/cript> ';
        }


        return $script;
    }
}
[/quote]

You are missing a piece of code to work right..
"if (is_array($src)){foreach ($src as $k=>$v){"

Code:
if ( ! function_exists('script_tag'))
{
    function script_tag($src = '', $type = 'text/javascript', $index_page = FALSE)
    {
        $CI =& get_instance();
        $script = '<s/cript';
        if (is_array($src)){
            foreach ($src as $k=>$v){
            {
                if ($k == 'src' AND strpos($v, '://') === FALSE)
                {
                    if ($index_page === TRUE)
                    {
                        $script .= ' src="'.$CI->config->site_url($v).'" ';
                    }
                    else
                    {
                        $script .= ' src="'.$CI->config->slash_item('base_url').$v.'" ';
                    }
                }
                else
                {
                    $script .= "$k=\"$v\" ";
                }
            }

            $script .= "></s/cript>";
        }
        else
        {
            if ( strpos($src, '://') !== FALSE)
            {
                $script .= ' src="'.$src.'" ';
            }
            elseif ($index_page === TRUE)
            {
                $script .= ' src="'.$CI->config->site_url($src).'" ';
            }
            else
            {
                $script .= ' src="'.$CI->config->slash_item('base_url').$src.'" ';
            }

            $script .= 'type="'.$type.'"';

            $script .= '></s/cript> ';
        }


        return $script;
    }
}
#8

[eluser]ShoeLace1291[/eluser]
You also have to add an extra closing bracket (}) after the first $script .= ">[removed]"; I got an unexpected t else error and adding that bracket fixed it.




Theme © iAndrew 2016 - Forum software by © MyBB