<< back to knowledgebase home

Modifying a MenuMachine 2 menu with PHP (MM2)

Article Applies To:

MenuMachine 2 only

GoLive CS, CS2

Mac, Windows

Article ID: 000144

Category: General Issues

Issue

Is it possible to dynamically change a MenuMachine 2 menu using PHP?

Solution

It is possible to do this but it is slightly involved. MenuMachine 2 is not designed to be easily editable at a source level and we don't publish documentation for doing so. If you want to attempt this, we have provided some basic pointers however we cannot provide support if you choose to proceed.

Getting PHP to process the menu definitions file

The site-wide nature of the menus makes things more complex because the actual menu definitions are stored in an external JavaScript file, which is not normally processed by the PHP parser.

For this reason you need to get PHP to process the menu definitions file as a PHP file before it is sent to the browser.

The menu definitions are stored in the menuspecs.js file which is located in the folder for your menu in the menumachine folder in the root of your site. There are two things that need to be done to make this file dynamic:

  1. get PHP to process the file
  2. make sure the browser does not cache the file


To get PHP to process the menuspecs.js file as a PHP file, you need to tell the web server to process .js files as PHP files.

You can do this on most Apache servers by creating a .htaccess file in the menumachine folder that contains the following:

AddType application/x-httpd-php .js

You *could* change the suffix of the menuspecs.js file to .php but MenuMachine writes .js by default and this will probably be more annoying since you'd need to continually re-link new pages to the .php file.

The only way to guarantee that the menu file is re-loaded each time the page loads is to get the web server to send an Expires header with the .js files.

If you are using Apache and the mod_expires module is loaded (which depends on your hosting provider) then you can add the following lines to the .htaccess file in the menumachine folder:

ExpiresActive on
ExpiresByType text/javascript "now plus 24 hours"

This will direct the web server to expire the content in one day, so the browser should request a new copy if it has not accessed the page for 24 hours.

You can use other time units:

ExpiresByType text/javascript "now plus 30 minutes"
ExpiresByType text/javascript "now plus 2 months"

etc.

Creating menus dynamically

We don't have publicly available documentation for the code, however here is a brief explanation.

The menu building code in menuspecs.js looks like so:

pkg.aB("block1","",...
pkg.aI("m2fft0pq","block1",...
pkg.aB("m2fft0ps","m2fft0pq",...

The lines atarting pkg.aB are creating menu blocks and the lines starting pkg.aI are creating menu items. The first text in quotes is the name of the block or item.

For blocks, the second piece of text in quotes is the name of its parent item (which is blank for the top-level block).

For items, the second piece of text identifies which block the item belongs to.

The parameters for the items start as follows:

name,block,width,height,x,y,text,textOver,url,urlTarget,image,imageOver,imageClick

The parameters for the blocks start as follows:

name,parent,width,height,x,y,xOffset,yOffset

All dimensions are stored as ems.

There are many other menu parameters but at this stage we don't provide information about them. We regret that we are unable to provide support for manual modification of the source code.

A future version of MenuMachine will have better support for server-side menu generation.

Last updated: July 23, 2008

This article has been viewed 4500 times.