Tutorial: Writing Custom Plugins

DOCS & TUTORIALS » Writing Custom Plugins


Since Xajax 0.5, you can extend Xajax by writing your own Xajax plugins. Currently Xajax supports two different types of plugins:
  • Request Plugins
  • Response Plugins

In this tutorial we are going to discover the possibilities of response plugins by creating a custom response plugin that provides a basic response API for scriptaculous / prototype.

Getting Started

Ensure that you have prototype and scriptaculous installed and working before using this example code verbatim.

Let's start with a simple plugin, then we'll expand it's capabilities. Add the following plugin code at the top of your index.php file (if you are using a single file implementation) or at the top of the index.server.php (if using an index, common and server setup).

Code: php

 
class scriptaculous extends xajaxResponsePlugin
{
    var $sCallName = "scriptaculous";
    
    function fade($id) {
        $this->_objResponse->script("new Effect.Fade(\"{$id}\");");
    }
    function appear($id) {
        $this->_objResponse->script("new Effect.Appear(\"{$id}\")");
    }
}
 
$pluginManager = &xajaxPluginManager::getInstance();
$pluginManager->registerResponsePlugin(new scriptaculous());
 



Now, you can use the plugin in your php code like this:

Code: php

 
function myPHPFunction() {
    $objResponse = new xajaxResponse();
 
    //PHP4 or PHP5 Plugin Syntax
    $objResponse->plugin("scriptaculous", "fade", "myDivID");
 
    return $objResponse;
}
 



While the syntax above will work in PHP5, there is an alternate syntax available in PHP5 ONLY that is more intuitive and readable:

Code: php

 
function myPHPFunction() {
    $objResponse = new xajaxResponse();
 
    //PHP5 ONLY Plugin Syntax
    $objResponse->scriptaculous->fade("myDivID");
 
    return $objResponse;
}
 



Adding On


At this point, you will start to think of all the nifty options that you are missing out on by settling for the simplistic version... so, here is an example of how to extend the plugin to support more of the scriptaculous options.

Let's extend the plugin a bit:

Code: php

 
class scriptaculous extends xajaxResponsePlugin
{
    var $sCallName = "scriptaculous";
 
    function processParameters(&$aParams) {
       //php5 users can replace the following code with json_encode();
        $comma = false;
        $params = ", {";
        if (isset($aParams["duration"])) {
            if ($comma)
                $params .= ", ";
            $params .= "duration: ";
            $params .= $aParams["duration"];
            $comma = true;
        }
        if (isset($aParams["queue"])) {
            if ($comma)
                $params .= ", ";
            $params .= "queue: '";
            $params .= $aParams["queue"];
            $params .= "'";
            $comma = true;
        }
        $params .= "}";
        if (", {}" == $params)
            $params = "";
        return $params;
    }        
    function fade($id, $aParams = array()) {
        $params = $this->processParameters($aParams);
        $this->_objResponse->script("new Effect.Fade(\"{$id}\"{$params});");
    }
    function appear($id, $aParams = array()) {
        $params = $this->processParameters($aParams);
        $this->_objResponse->script("new Effect.Appear(\"{$id}\"{$params})");
    }
}    
$pluginManager = &xajaxPluginManager::getInstance();
$pluginManager->registerResponsePlugin(new scriptaculous());
 



Now, you can use the plugin with the additional parameters like this:

Code: php

 
function myPHPFunction() {
    $objResponse = new xajaxResponse();
 
    //PHP4 or PHP5 Plugin Syntax
    $objResponse->plugin("scriptaculous", "fade", "myDivID", array("duration"=>".3"));
    $objResponse->plugin("scriptaculous", "appear", "myDivID", array("queue"=>"end"));
 
    return $objResponse;
}
 



Or using the PHP5 Only syntax:

Code: php

 
function myPHPFunction() {
    $objResponse = new xajaxResponse();
 
    //PHP5 ONLY Plugin Syntax
    $objResponse->scriptaculous->fade("myDivID", array("duration"=>".3", "queue"=>"end"));
    $objResponse->scriptaculous->appear("myDivID", array("duration"=>".8", "queue"=>"end"));
 
    return $objResponse;
}
 



Adding additional functions, using the above functions as templates, should be straightforward.

See also the basticPluginTest.php file that comes packaged with xajax 0.5.

 
DOCS & TUTORIALS » Writing Custom Plugins


XAJAX on SourceForge.net
Support this project Valid XHTML 1.0 Transitional