Welcome Guest, Not a member yet? Register   Sign In
HTML Helper Extended - script_tag() added (Add .js files easily)
#1

[eluser]Isern Palaus[/eluser]
Hello,

I was programming a website today (I was 2 months without touching any code) and I've seen that now CodeIgniter has a lot of features and one of them is the link_tag() for adding CSS files.

But... What about the JavaScript? Here is a literaly modification of the link_tag() for using with [removed]

File: /application/helpers/my_html_helper.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Script
*
* Generates a script inclusion of a JavaScript file
* Based on the CodeIgniters original Link Tag.
*
* Author(s): Isern Palaus <[email protected]>
*
* @access    public
* @param    mixed    javascript sources or an array
* @param    string    language
* @param    string    type
* @param    boolean    should index_page be added to the javascript path
* @return    string
*/    

if ( ! function_exists('script_tag'))
{
    function script_tag($src = '', $language = 'javascript', $type = 'text/javascript', $index_page = FALSE)
    {
        $CI =& get_instance();

        $script = '$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 .= ">\n";
        }
        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 .= 'language="'.$language.'" type="'.$type.'"';
            
            $script .= '>'."\n";
        }

    
        return $script;
    }
}
?&gt;

We only need to write...

Code:
&lt;?=script_tag('content/js/jquery-latest.js');?&gt;

This is my first CodeIgniter aportation, wish you like it.

Sorry for my school english :-).

Regards,
-- Isern Palaus
#2

[eluser]Kilroy[/eluser]
Hi,

I found the above code to be incomplete (missing some stuff around line 25), and it didn't add a [removed] at the end. Here's a reviewed function:

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Script
*
* Generates a script inclusion of a JavaScript file
* Based on the CodeIgniters original Link Tag.
*
* Author(s): Isern Palaus <[email protected]>, Viktor Rutberg <[email protected]>
*
* @access    public
* @param    mixed    javascript sources or an array
* @param    string    language
* @param    string    type
* @param    boolean    should index_page be added to the javascript path
* @return    string
*/    

if ( ! function_exists('script_tag'))
{
    function script_tag($src = '', $language = 'javascript', $type = 'text/javascript', $index_page = FALSE)
    {
        $CI =& get_instance();

        $script = '&lt;script ';
        
        if(is_array($src))
        {
            foreach($src as $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 .= ">\n";
        }
        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 .= 'language="'.$language.'" type="'.$type.'"';
            
            $script .= '>'."\n";
        }

        
        $script .= '&lt;/script&gt;';
        
        return $script;
    }
}

?&gt;
#3

[eluser]Blaze Boy[/eluser]
Isern Palaus:
take some of your time to check my project (Vunsy) it's a project based on codeigniter to deliver a site in some hours without touching a code
http://www.vunsy.co.cc
it has the syntax that you need of adding javascript and css files in a click like that
add('path/to/cssfile.css' );
add('path/to/jsfile.js' );
add( '[removed]' );

that is a smart function it can determine what fort of file you need to add to header
check it out i think you'll like it Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB