API / Examples / Hello World
Modules can be used as extensions too which means you can write small codes that extend/modify your website without having to write full length modules. They will act just like a module, you will be able to install them from Module Manager and they will show up in your Installed Modules list too (but this may change in the future releases).
So for e.g. take the following code to echo out "Hello World!" to every page:
<?php
/***************************************************************
*
* Name: Hello World Extension
* URI: http://jcore.net
* Description: A simple module that can be used for extensions.
* Author: Istvan Petres
* Version: 1.0
* Tags: extension, module, hello world, gpl
*
***************************************************************/
class helloWorld extends modules {
static function displayHello() {
echo "Hello World!";
}
}
modules::register(
'HelloWorld',
_('Hello World'),
_('A simple extenision to jCore using the API'),
'application-default-icon.png');
// This will print out "Hello World!"
// on the bottom of every page
api::addHook('pages::display', 'helloWorld::displayHello');
?>
Save this file to helloworld.class.php in your lib/modules/ directory and go to Admin -> Module Manager to activate it and that's it.
The forth parameter in the modules::register function can be any icon from the lib/icons/48/ directory, or you can use any url (for e.g. http://domain.com/icon.png) for your icon. It is recommended to use url::site() to point to your domain as this way the extension will work with other domains too.
You can also print out "Hello World" on the top of the pages too, simple by using this API call:
api::addHook(API_HOOK_BEFORE, 'pages::display', 'helloWorld::displayHello');
There is also the possibility to have the API return you all the content of the display function and then you can use str_replace or preg_replace to replace any part of the content with your own. In these cases the first argument of your hook function will be the content returned so for e.g. your helloWorld::display() function will look like helloWorld::display($content) { ... }.
In the case of API_HOOK_RETURN it is up to you to echo out the $content or to totally replace it with something but if you do want to totally replace it I recommend using return in your display function with the API_HOOK_BEFORE as this way the hooked to function won't run all the codes to generate the output so you save resources.
api::addHook(API_HOOK_RETURN, 'pages::display', 'helloWorld::displayHello');
In one module / template / extension you can use as many hooks as you wish so you don't need to create separate modules / extensions for each of your changes, just put them into one and that's it.