4. My First Module

info To continue learning about modules, please download demo module. The following information will be based on demo module.

To create your module, first start with windowed mode, unless you don't need one. This chapter will cover windowed mode module.

Windowed mode requires the following files:

  • /module/[mod_name]/window.php
  • /skins/[skin_name]/module_[skin_file].tpl, if required

Hello World

<?php
$output = 'Hello World, as usual...';
?>

To call this module, simply add the following line to any of your .tpl file:

<!-- BEGINMODULE mod_name --><!-- ENDMODULE -->

That's simple right? In windowed mode, all output must be contained in $output variable.

Hello {mod_ini}

Now, get something more fun, passing variables to module from .tpl (not from another .php!). Begin with adding some variable to your .tpl:

<!-- BEGINMODULE mod_name -->
; remark, will be ignored
; variable name must be: a-z,_,0-9, no spaces
; you can use anything as value, including spaces
my_name = Bill Gates
<!-- ENDMODULE -->

In your window.php, call the variable by checking $mod_ini variable, eg:

<?php
$name = $mod_ini['my_name'];
$output = 'Hello, '.$name; // output: Hello, Bill Gates
?>

Easy eh? $mod_ini is especially useful to allow you to customize modules on the fly. For example, in qE package, you have page_gallery module, and in welcome.tpl, you see:

<!-- BEGINMODULE page_gallery -->
; Welcome text
page_id = 1
body = 1
<!-- ENDMODULE -->

This tell the module to load page content from page_id 1, and only display the body.

Hello {mod_config}

The other way to pass variables is by using module configuration values. The values are stored in database instead of .tpl. This is not as flexible as mod_ini, but you can use it to store module configurations.

There are several steps involved to use this method.

  1. Store the values in database, exactly in [db_prefix]_module_config table.
  2. You can store it in several ways, but qE already provide a way to so in module installation step:
    <config>
    <configId>example</configId>
    <configValue>123</configValue>
    </config>
  3. That will store  '123' as value for 'example'.
  4. Then call the $module_config[mod_name], eg $module_config['demo'].

Sample code:

<?php
$name = $module_config['demo']['example'];
$output = 'Hello, '.$name; // output: Hello, 123
?>

There is no comment. Why not be the first?

More Comments/Post Your Own

Advertisement

Online Users

There are 4 users online.

Connect

Connect with us on Facebook

Newsletter

Subscribe to our newsletter for the latest updates and exciting promotions!