# Writing modules for use with Contenido

Author | Timo A. Hummel
Created | 3rd November 2003
Audience | Module Developers
Applies to | Contenido 4.4 or later

## Introduction

Writing modules with Contenido is as easy as writing regular PHP code. You don't get a stripped-down development environment, but rather you get a combination of Contenido's CMS features combined with your own code.

## What's input, what's output, what's a module anyways?

A module is nothing more than a piece of code which gets combined using a template:
As you already know, a layout contains placeholders for modules. The PHP-Code from the module's output sections are inserted into the correct positions in the layout. The input section is not relevant for the output, but the input section is relevant for configuring the modules.

## Configuration magic

To make modules more "modular", you can configure them. You might have seen this in the predefined modules - but how does it work?
The configuration data is stored transparently, so you don't need to worry to get your configuration data from the input section to the output section. Storing configuration data is pretty easy. Take a look at this simple example of an input section:

    echo " <table>
              <tr>
                <td>Test</td>
                <td><input type=\"text\" name=\"CMS_VAR[1]\" value=\"CMS_VALUE[1]\"></td>
              </tr>
           </table>";

CMS_VAR[x] is the variable (which is set using "magic" :)) to "receive" the desired value, and CMS_VALUE[x] is the current value. The same story goes for the output area:

    echo "CMS_VALUE[1]";

To avoid problems, you should always take care of the following things:

- Always store your configuration into local variables (e.g. $foo = "CMS_VALUE[1]";)
- Always use quotes around CMS_VALUE, as CMS_VALUE is text.
- PHP allows transparent type switching, so the following code will also work:

      $foo = "CMS_VALUE[1]";

          if ($foo == 1)
      {
          echo "Worked";
      }

## Important System stuff

If you want to write advanced modules, you might want to interact with Contenido. Here are a few objects and functions you can use with your modules.

DB_Contenido | The well-known PHPLib DB-Object. Example: $mydb = new DB_Contenido; $mydb->query("SELECT * FROM test"); Reference: See http://www.sanisoft.com/phplib/manual/DB_SqlMethods.php#DB_SqlMethodsInt You should always use $cfg["tab"][*] to access Contenido tables. Warning: You should never ever use $db in your modules. Always use your own variables.
$sess | The current session. Useful variables: $sess->id (current session ID)
$auth | Authentification information. Useful variables: $auth->auth["uid"] (current user ID) $auth->auth["uname"] (current user name)