JQuery_ext Class (version 1.0)
JQuery_ext class for Codeigniter helps inserting and inflating JQuery or general javascript code into your projects created usign this wonderful PHP Framework.
Provides also an easy and friendly way to add javascript libraries and plugins in pages.
Optionally can also minify the javascript code and/or generate on the fly a javascript file included in pages.
Example
Here is a simple example showing how to open a JQueryUI dialog in one of your controller functions:
$this->load->library('jquery_ext');
$script = '$("#dialog").dialog({
title: "Jquery Ext Demo",
modal: true,
buttons: {
Ok: function() {
$( this ).dialog( "close" );
},
},
width: 600,
height: 300
})';
$this->jquery_ext->add_script($script);
Notes:
The $script string contains your javascript code. It is passed to the $this->jquery_ext->add_script function as shown above.
NOTE: Be sure to insert the call to the function $this->jquery_ext->output() in your views where the library must put the generated code and the scripts inclusions (usually before the </head> closing tag). Otherwise the library cannot know where to put the generated output.
Configuring the Library
The following is a list of all the preferences you can set.
$config['jquery_ext']['auto_insert_jquery'] = TRUE;
Specifies if the library must auto insert the JQuery standard library when it's loaded.
$config['jquery_ext']['minimize_output'] = TRUE;
Specifies if the library must minify the javascript code (Using JSMin-PHP by Douglas Crockford).
$config['jquery_ext']['main_library_path'] = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js';
Specifies the JQuery main library URL (can be an external or local).
$config['jquery_ext']['libraries_prefix'] = "js/";
Prefix to prepend to URL for libraries loaded from local js folder.
$config['jquery_ext']['generate_js_files'] = TRUE;
Specifies if the javascript code must be embedded directly into the page or stored in a js file included using script tag "src" attribute.
NOTE: If you use js files take care of cleaning obsolete files using often $this->jquery_ext->garbage_collector function or a custom garbage collector that removes old files. Note also that this method can easily generate a huge amount of files if your website as a lot of page views.
$config['jquery_ext']['js_files_fs_path'] = 'js/output/';
Path where to store js files relative to your document root.
$config['jquery_ext']['js_files_url_prefix'] = config_item('base_url') . 'js/output/';
URL to include js files generated by the library.
$config['jquery_ext']['libraries'] = array();
In this array you can add every library you wanna use in your application using $this->jquery_ext->add_plugin function.
$config['jquery_ext']['libraries']['jquery_ui'] = array(
"path" => 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/',
"files" => array (
'jquery-ui.min.js',
'themes/cupertino/jquery-ui.css'
)
);
$config['jquery_ext']['autoload'] = array('jquery_ui');
List of libraries to autoload dinamically when the class is loaded.
Adding JQuery Main Library manually
If you would like to manually add the JQuery library to the page just call:
$this->jquery_ext->add_jquery();
Anywhere in your controllers.
How to add a generic javascript script inclusion
The library js file can be on local filesystem or a full URL. Example:
$this->jquery_ext->add_library("/js/library_name.js");
// or
$this->jquery_ext->add_library("http://example.com/js/library_name.js");
How to add a CSS inclusion
The css file can be on local filesystem or a full URL. Example:
$this->jquery_ext->add_library("/css/style.css");
// or
$this->jquery_ext->add_library("http://example.com/css/style.css");
How to load a plugin
$this->jquery_ext->add_plugin($plugin_name);
Loads and add to the output a plugin. $plugin_name must contain a valid plugin name configured in the library config file.
$this->jquery_ext->add_plugin('jquery_ui');
How to inflate a simple script to the javascript code
// a simple js script
$script = "function test()
{
alert('hello world!');
}";
// adds the script to the document ready jquery function
$this->jquery->add_script($script,"document_ready");
Adds a script to the output. The first parameter is the script and the second parameter is the function where the script must be included.
NOTE: You can specify new javascript function configuring properly the dictionary in the jquery_ext config file. By default general and document ready are available.
How to run old js files garbage collector
If you use js files take care of cleaning obsolete files using often $this->jquery_ext->garbage_collector function or a custom garbage collector that removes old files.
This function can accept an optional timespan parameter that specify the number of minutes for deleting a file.
NOTE: This function is not called automatically, so you must call it manually when needed.
Credits and more information
This library is not part of the Codeigniter PHP Framework, was created by Alberto Sabena and is available "as is it" without any warranties.
The project main page is: http://code.google.com/p/codeigniter-jquery/, please help us reporting bug or contributing with your own code or suggestions.
Minify function is provided by JSMin-PHP library by Douglas Crockford.