UPB2 Hello World

From MyUPBWiki

Jump to: navigation, search

Often when introducing a new way of doing something in programming we always start off with a "Hello World" application, so thats exactly what we will do here.

Contents

Getting Started

The first thing you will need of course is a copy of UPB, which can be obtained from the UPB fileserver. Once you extract the zip file you should have a directory structure much like the one below.

upb2
   |
   +--includes
   |  |
   |  +--class
   |  |  |..
   |  +--commands
   |  |  |..
   |  |--config.php
   |  
   +--skins
   |  |..
   |  
   |--index.php

Creating the Module

Lets start by writing some code and anaylze whats happening.

The Code

Open up your favorite text editor and create a file called helloworld.php, you should save this file in the upb2/includes/commands directory.

<?php
/**
 * Simple Hello World module
 */

class upbPage extends upb {
    function upbPage($command) {
        $this->upb($command);
        $this->tpl_loadTemplate("helloworld.tpl");

        $this->tpl_set(array("text" => "Hello World"));

        $this->_upb();
    }
}
?>

How does it work

If you are not familiar with object orientated programming this might look fairly odd to you. But, whatever the case i will explain line by line.

class upbPage extends upb {

We are creating a class that "extends" the functionality of the upb class. The upb class is the heart of the forums, it provides the framework which allows you to develop upb modules. The class name "upbPage" is manditory. When your module is run an instance of upbPage is created, this is done automatically and is why this must remain the same in each module.

function upbPage($command) {

If your new to object orientation your probably wondering when does the function upbPage get called? In PHP4, having a function within a class having the same name as the class is called the "class constructor". It is actually called automatically when an instance of the class is created. So, now we have an entry point for the module, this is where code will start to be run.

$this->upb($command);

In order to use the framework setup in the upb class we actually have to call the constructor function for the parent class. This function sets up all our objects/variables so we can easily access all the functionality of the upb framework.

$this->tpl_loadTemplate("helloworld.tpl");

Since not every page has output we must manually load in a template. It is best practice to name your template the same name as your module. I will talk about how to make this template in the next section.

$this->tpl_set(array("text" => "Hello World"));

Heres where we finally write our own code. We call a function of the tpl class which sets a variable class "text" to the value of "Hello World". Now this actually means nothing until we actually create the HTML template for this module which I will explain in the next section.

$this->_upb();

Since PHP4 does not support deconstructors, we have used a naming convention to signify this. This line calls the deconstructor for the upb class and is required for your module to function.

Creating the Template

In order for our module to display anything to the user we need to setup some HTML rules. We do this using HTML template files and integrate it into our module using the tpl (short for template) class

The Template

Go ahead and open up any old text editor and save a blank file called helloworld.tpl into the upb2/skins/myupb/tpl directory. If you want this module to work in another skin then put it in the appropriete directory, but for now we will just create a template file for the default skin. Type the following into your new file.

Welcome to my custom module!<br/>
{text}

How does it work

A template file is just simply a html file but with some extra little features. You have probably already guessed that {text} is a variable. We defined this earlier in helloworld.php There is many other neat things you can do inside your template to make displaying things really easy. Check out the template reference for more info!

Running our module

After all that we can now run our module. Load up your favorite webbrowser and navigate to where you installed upb2. When you go over to upb2 the main forum listing should load up, this is index.php with no querystring. To load our module type in index.php?command=helloworld and you should see the following.

Welcome to my custom module!
Hello World

Where to go next

Since you are familiar with creating a basic module you can move on to more advanced topics, check out some of the other articles in the UPB2 Developers Guide