about me

I'm a 34'ish year old web application developer from South Portland, Maine. I love meeting fellow techies, drop me a line if you want to talk shop.

Categories

CodeIgniter Javascript Library (PHP5)

Posted on 10/10 at 03:49 PM

I wrote this library as a means of knowing which Javascripts to load in my view for a given controller/method. My sites often use a common array of jquery libraries, but occasionally I need to use a special JS script on a certain page, say ... for table sorting or something.

In order to use this library, PHP5 is required, though it can easily be altered for PHP4. First create a file called ‘Javascripts.php’ in your ‘application/liraries’ folder with the following contents:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Javascripts:: a class that stores an array of javascripts to be output in the view
*
* @author  Russell Jones - CodeOfficer <spam@codeofficer.com>
* @url http://www.codeofficer.com/
* @version 1.0
* looks in your config for: $config['javascripts'] = array('jquery.js', 'interface.js'); 
*/

class Javascripts
{
    
    private $javascripts
;
    
private $CI;

    function 
__construct($params=NULL)
    
{
        $this
->clear();
        
$this->CI =& get_instance();
        
$config $this->CI->config->item('javascript_files');
        if (
$config$this->add($config);
        if (
$params$this->add($params);
    
}

    
// clear all javascripts
    
function clear()
    
{
        $this
->javascripts = array();
    
}

    
// add a javascript
    
function add($items)
    
{
        
if (is_array($items)) {
            
foreach ($items as $item{
                
if (!in_array($item$this->javascripts)) {
                    $this
->javascripts[] $item;
                
}
            }
        } 
else {
            
if (!in_array($items$this->javascripts)) {
                $this
->javascripts[] $items;
            
}
        }
    }

    
// return the array of javascripts
    
function get()
    
{
        
return $this->javascripts;
    
}

    
// output the array of javascripts
    
function to_string()
    
{
        
return 'javascripts are: '.implode(','$this->javascripts);
    
}
}
?>

Then in your config file, specify an initial array of JS files to load, ones that you might use regularly throughout your entire site.

$config['javascripts'= array('jquery.js''interface.js');

Remeber to autoload the Javascripts library along with any other libraries we might be using regularly.

$autoload['libraries'= array('javascripts');

Now in each of you controller/methods you can add additional JS files to the stack that will only load for that particular controller/method.

$this->javascripts->add('some_new_script.js');

or 
even ...

$this->javascripts->add('some_new_script.js''yet_another_new_script.js');

The last thing you will do is pass the array of JS file names to your header template like so:

// mind you, I use smarty as my View layer but the logic is the same
$this->mysmarty->assign('javascripts'$this->javascripts->get());

Keep in mind, I use smarty as my view layer, but this code can easily be adapted to work with CI’s view. I make the assumption that your view knows what directory all your JS files will be in, and that they are all in the same directory. Then in your header template, use:

<!-- Framework JS files -->    
{foreach item=javascript from=$javascripts}
<script src="/common/js/{$javascript}" type="text/javascript"></script>
{
/foreach}

which will result in something like 
...

<!-- 
Framework JS files -->    
<
script src="/common/js/jquery.pack.js" type="text/javascript"></script>
<script src="/common/js/jquery.form.pack.js" type="text/javascript"></script>
<script src="/common/js/jquery.metadata.pack.js" type="text/javascript"></script>
<script src="/common/js/jquery.validate.pack.js" type="text/javascript"></script>

4 Comments

Comment #1 by CodeOfficer  on  11/03  at  03:19 PM

As noted above, I use Smarty for my view layer, but one could easily replace all the smarty specific sections of my code with PHP equivalents.

$data['javascripts'$this->javascripts->get();

<? foreach ($javascripts as $script): ?><?=$script?><? endforeach; ?>

Comment #2 by Ignacio  on  04/28  at  09:23 PM

Thanks!!

Comment #3 by Ignacio  on  04/28  at  09:36 PM

function get() {
 $scripts 
"";
 foreach(
$this->javascripts as $script{
  $scripts 
.= sprintf(''BASE $this->CI->config->item('js_path') . $script)."\n";
 
}
 
return $scripts;
}

Comment #4 by CodeOfficer  on  04/29  at  04:30 PM

Glad you found it useful Ignacio smile

Leave a comment?

Name:

Email:

Location:

URL:

Remember my personal information

Notify me of follow-up comments?

Submit the word you see below:


RailsConf 2008 Often times I will release code for free or go that extra distance to help others online. If my skills were useful to you, please consider a small donation. Thank you very much.

recommend me!

Search

You Can Find Me

@ github.com
@ twitter.com

My Wishlists

@ Amazon.com

My Other Sites

Foundation's Edge, RJones Family, We're Not.com (only for staging), Ailee Jones (same as rjones for now)

Friends of Mine

Aaron, Barnaby, Brian, Chris, Dirk, Frank, Fred, Four, Justin, Matt, Mike, Monty, Paul, Sean, Travis

IRC Hangouts

I can usually be found lounging on irc.freenode.net while I work, on the following channels: #fauna, #github, #hello-heroku, #jquery, #passenger, #ruby, #rubyonrails, #slicehost, #sproutcore, #textmate, #werenot.