DHTML Menu Builder [DynAPI 1.1]
<developers_edition>
Tuesday May 20, 2008

 

 

WARNING: The information on this documentation will change as the development on this version of DHTML Menu Builder progresses.
It is VERY important that you always refer to the information provided in this document prior to testing new versions of the DynAPI code.


Introduction Tutorial

This tutorial will guide you through the most basic steps in creating your first dynamic menu using DHTML Menu Builder <developers' edition> and the DynAPI.

Introduction

There are two main scenarios in the process of creating dynamic menus:

Although this tutorial will not address any of these scenarios exclusively, the process is exactly the same for both and can be summarized by these steps:

Each one of these steps will be covered in this tutorial.

Preparation

If you have already implemented a static menu into your web site you can skip this section.

The first thing you must know is that before you can use the DynAPI you need to have a working and fully implemented project running on your web site.
So, if you have not yet created a project, please follow these steps:

This is the basic procedure to get a project implemented on your web site; of course, more detailed instructions can be found on DHTML Menu Builder's documentation:

Note that it is not the intention of this tutorial to cover the implementation of a menu project. This is very well covered in DHTML Menu Builder's main documentation. So follow the previous steps in case that you need to get a project implemented in your web site.

Enabling DynAPI Support

DHTML Menu Builder <developers' edition> is exactly the same product as the Standard Edition with the only exception that can embed the DynAPI into the javascript files it generates. As you can imagine, we need this DynAPI in order to be able to use it, so here's how to prepare a project so that when compiled, the DynAPI is included in the generated javascript files:

Adding DynAPI code

The final step of the process is to add DynAPI code to your HTML documents. This code will contain calls to the DynAPI set of functions that will allow you to add new items to your menus.
The DynAPI not only contains functions to add new items, but contains functions to perform many different operations (such as modifying existing items, removing items, etc...) but in this tutorial we will concentrate on the most common use of the DynAPI which is the addition of new items.

To add new items the DynAPI provides two set of functions:

The low level functions provide much more control over the way new items are added but they are also considerably harder to use and understand so, for this tutorial, we will use the high level function dmbAPI_parseArray.
You will notice that throughout this section of the tutorial, DynAPI functions will be hyperlinks and clicking on them will take you to the documentation for that specific function.

DynAPI code is nothing more than a set of javascript instructions so the first thing you need to do is:

<script language="javascript" type="text/javascript">
    dmbAPI_Init();
</script>

Every single portion of DynAPI code must be enclosed between <script> tags, correctly identified with the "language" and "type" parameters as a "javascript" block.
The first block of DynAPI code (in case that you have several) must always call the dmbAPI_Init function before attempting to call any other DynAPI function.

Now that we have our basic DynAPI code block we can begin to perform calls to the dmbAPI_parseArray function to add new menu items.

The dmbAPI_parseArray function is extremely easy to use and the only thing that you need to master is the way menu items are identified. Nothing better than to create a sample and then we'll analyze the code line-by-line.

Let's suppose that we want to create this menu structure:

Although this menu looks simple it will allow you to fully understand how the dmbAPI_parseArray function works.

Here's the first portion of the code, which will create the main menu items, that is, the items that will appear on the toolbar:

<script language="javascript" type="text/javascript">
    dmbAPI_Init();

    dmbAPI_parseArray(1, 0, "Home", "/index.php");
    dmbAPI_parseArray(2, 0, "Products");
    dmbAPI_parseArray(3, 0, "Contact Us", "/ss.php");
</script>

As you may have noticed, a call to the dmbAPI_parseArray function involves passing a series of parameters:

Let's now add the remaining menu items:

<script language="javascript" type="text/javascript">
    dmbAPI_Init();

    dmbAPI_parseArray(1, 0, "Home", "/index.php");
    dmbAPI_parseArray(2, 0, "Products");
    dmbAPI_parseArray(3, 0, "Contact Us", "/ss.php");

    dmbAPI_parseArray(21, 2, "DHTML Menu Builder");
    dmbAPI_parseArray(211, 21, "Standard Edition", "/utilities/dmbuilder/index.php");
    dmbAPI_parseArray(212, 21, "developers&#39; edition", "/utilities/dmbuilderde/index.php");
    dmbAPI_parseArray(213, 21, "LITE", "/utilities/dmbuilderlite/index.php");
   
    dmbAPI_parseArray(22, 2, "KeyLaunch");
    dmbAPI_parseArray(23, 2, "SoftLeds");
</script>

Don't worry if, all of a sudden, everything looks too complicated -- it is not. Let's analyze each new line:

 dmbAPI_parseArray(21, 2, "DHTML Menu Builder");

In this line of code you can see that the item ID is "21" and the parent ID is "2".
The items IDs can be anything you like (as long as they are unique) so you can be creative with them so that they not only server as unique identifiers but they also help you recognize the menu items' hierarchy in the menus' structure.

Setting the ID to "21" allows us to easily identify that this is a subitem: a subitem of the item "2" and this is the first subitem in the list "1" thus "2" and "1" gives us "21".
The second parameter is easier to understand and we are simply saying that the "DHTML Menu Builder" item should be a subitem of "Products" simply because the "Products" item's ID is "2".

The next item:

dmbAPI_parseArray(211, 21, "Standard Edition", "/utilities/dmbuilder/index.php");

Again, here we can identify that this is a third-level sub item (its ID has three digits) and it's obviously the first subitem. The first two digits of the ID ididicate to which item it belongs "21", the last one its place in the hierarchy.

If all this numbering technique confuses you, here's an alternate version of the code:

<script language="javascript" type="text/javascript">
    dmbAPI_Init();

    dmbAPI_parseArray("menu_Home", 0, "Home", "/index.php");
    dmbAPI_parseArray("menu_Products", 0, "Products");
    dmbAPI_parseArray("menu_ContactUs", 0, "Contact Us", "/ss.php");

    dmbAPI_parseArray("menu_Products_DHTMLMenuBuilder", "menu_Products", "DHTML Menu Builder");
    dmbAPI_parseArray("menu_Products_DHTMLMenuBuilder_SE", "menu_Products_DHTMLMenuBuilder", "Standard Edition", "/utilities/dmbuilder/index.php");
    dmbAPI_parseArray("menu_Products_DHTMLMenuBuilder_DE", "menu_Products_DHTMLMenuBuilder", "developers&#39; edition", "/utilities/dmbuilderde/index.php");
    dmbAPI_parseArray("menu_Products_DHTMLMenuBuilder_LITE", "menu_Products_DHTMLMenuBuilder", "LITE", "/utilities/dmbuilderlite/index.php");
   
    dmbAPI_parseArray("menu_Products_KeyLaunch", "menu_Products", "KeyLaunch");
    dmbAPI_parseArray("menu_Products_SoftLeds", "menu_Products", "SoftLeds");
</script>

In this version, instead of using numbers we are using text for both the menu items' ID and, consequently, to identify their parents.
If you are completely new to the DynAPI this approach may look easier to understand but you should give the other approach a try -- although this method is completely valid you can imagine that with a rather large project you will end having extremely long lines of code which will become hard to maintain and will consume unnecessary extra bandwidth when your web site is visited.


I hope this introduction to the DynAPI has been helpful and, as always, don't hesitate to contact me if you have to have a question or encounter any problems.