Skip to content
Patrick Upson edited this page Jun 4, 2014 · 2 revisions

A constants file contains arrays of menu items denoting how each element in the menu is linked to another. It's important to give the constants file, as well as the variables defined in the file, unique names as it makes referencing the item easier when generating a unique left side menu or landing page through out the site. Below is an example of how to construct a constants file.

Table of Contents

Menu Variable

First what does a menu variable look like. As personal preference I like to prefix menu items with m_ to indicate it's a menu item and make it less likely the variable will conflict with variables used throughout the website. A menu item has three basic arrays (name, address, links) this is what a variable definition would look like. Note I've named it "$m_mysitemenu", but it could be called whatever you like:

<?php
//TODO: Ask someone nicely to provide a real translation for "My website"
$m_mysitemenu['name'] = array('en' => 'My website', 'fr' => 'mon site web');
$m_mysitemenu['addr'] = '/root/of/my/site/page';
$m_mysitemenu['links'] = array();
?>
The variable could also be defined as:
<?php
$m_mysitemenu['name']['en'] = 'My website';
$m_mysitemenu['name']['fr'] = 'mon site web';
$m_mysitemenu['addr'] = '/root/of/my/site/page';
$m_mysitemenu['links'] = array();
?>

$_mysitemenu['name']

'name' defines the human readable name of the menu element. This is the text that will appear on the page. It is also an array containing the two letter language extensions 'en' and 'fr' this is so the variable can be referenced using the PHP Variants $_PAGE['lang1'] variable as in echo $m_mysitemenu['name'][$_PAGE['lang1']]; , which will display the proper name for the menu item for the language of the page it's being displayed on. Other language extensions can also be used, but they must match languages supplied by $_PAGE['lang1'].

$m_mysitemenu['addr']

'addr' defines the address this menu item should point to. By only using 'addr' with out specifying the '-en.php' or '-fr.php' postfixes the menu utility will assume addresses starting with '/' are local to the site and also assumes the address ends in '-en.php' or '-fr.php'. The language postfix is chosen based on the PHP variants $_PAGE['lang1'] variable. There are alternative ways to specify the address for more complex usages such as:

//TODO: ask someone nicely to translate '/root/of/my/site/page-fr.php' to a full french path to demonstrate
//French and English paths could belong to entirely different parts of the site.
$m_mysitemenu['addr'] = array('en' => '/root/of/my/site/page-en.php', 'fr' => '/root/of/my/site/page-fr.php');
or
$m_mysitemenu['addr']['en'] = '/root/of/my/site/page-en.php';
$m_mysitemenu['addr']['fr'] = '/root/of/my/site/page-fr.php';
If the address begins with "http", assumed to be an external page, or "#", assumed to be an anchor, language postfixes are not appended to the address.

$m_mysitemenu['links']

'links' is an array of child menu items. There are multiple ways to add items to the list, I prefer to set the variable as an array first then use array_push($m_mysitemenu['links'], $m_mysitemenu_link); to add items after each $m_mysitemenu_link is defined in the file demonstrated below:

<?php
$m_mysitemenu['name']['en'] = 'My website';
$m_mysitemenu['name']['fr'] = 'mon site web';
$m_mysitemenu['addr'] = '/root/of/my/site/page';
$m_mysitemenu['links'] = array();

$m_mysitemenu_link1['name']['en'] = 'link1(en)';
$m_mysitemenu_link1['name']['fr'] = 'link1(fr)';
$m_mysitemenu_link1['addr'] = '/root/of/my/site/link1';
array_push($m_mysitemenu['links'], $m_mysitemenu_link1);

$m_mysitemenu_link2['name']['en'] = 'link2(en)';
$m_mysitemenu_link2['name']['fr'] = 'link2(fr)';
$m_mysitemenu_link2['addr'] = '/root/of/my/site/link2';
array_push($m_mysitemenu['links'], $m_mysitemenu_link2);

$m_mysitemenu_link3['name']['en'] = 'link3(en)';
$m_mysitemenu_link3['name']['fr'] = 'link3(fr)';
$m_mysitemenu_link3['addr'] = '/root/of/my/site/link3';
array_push($m_mysitemenu['links'], $m_mysitemenu_link3);

$m_mysitemenu_link4['name']['en'] = 'link4(en)';
$m_mysitemenu_link4['name']['fr'] = 'link4(fr)';
$m_mysitemenu_link4['addr'] = '/root/of/my/site/link4';
array_push($m_mysitemenu['links'], $m_mysitemenu_link4);
?>
The alternative is to create the $m_mysitemenu['links'] variable at the end of the file and add all links to it then, as demonstrated below:
<?php
$m_mysitemenu['name']['en'] = 'My website';
$m_mysitemenu['name']['fr'] = 'mon site web';
$m_mysitemenu['addr'] = '/root/of/my/site/page';

$m_mysitemenu_link1['name']['en'] = 'link1(en)';
$m_mysitemenu_link1['name']['fr'] = 'link1(fr)';
$m_mysitemenu_link1['addr'] = '/root/of/my/site/link1';

$m_mysitemenu_link2['name']['en'] = 'link2(en)';
$m_mysitemenu_link2['name']['fr'] = 'link2(fr)';
$m_mysitemenu_link2['addr'] = '/root/of/my/site/link2';

$m_mysitemenu_link3['name']['en'] = 'link3(en)';
$m_mysitemenu_link3['name']['fr'] = 'link3(fr)';
$m_mysitemenu_link3['addr'] = '/root/of/my/site/link3';

$m_mysitemenu_link4['name']['en'] = 'link4(en)';
$m_mysitemenu_link4['name']['fr'] = 'link4(fr)';
$m_mysitemenu_link4['addr'] = '/root/of/my/site/link4';

$m_mysitemenu['links'] = array($m_mysitemenu_link1, $m_mysitemenu_link2, $m_mysitemenu_link3, $m_mysitemenu_link4);
?>