Jump to content

Automatic Menu Generation


MeOnTheWeb

Recommended Posts

Hello,

I've got a text file that contains a menu structure:

HEADER 1

ITEM1 > ITEM1-1.HTML (or PHP)

ITEM2 > ITEM1-2.HTML (or PHP)

HEADER 2

ITEM1 > ITEM2-1.HTML (or PHP)

ITEM2 > ITEM2-2.HTML (or PHP)

ITEM3 > ITEM2-3.HTML (or PHP)

I'd like to use Javascript to build this menu, so that if I make a change (either add or delete to the text file) it is reflected in the web application. The menu should be vertical, left-aligned with the content appearing on the right pane.

+----------+---------------------------------------------+
|  ITEM  |   CONTENT                              |

|             |                                                    |

|             |                                                    |

+----------+---------------------------------------------+

Can someone direct me to a library that could do something like this?

Thx,

M

Link to comment
Share on other sites

Do it in the HTML directly. It'll save you a lot of time and effort.

If you want to explore how one might do this academically, then I would say to forget text files and use something that's easy for you to read and write and especially easy for a computer to read: JSON. There are a number of ways to get that data into the page, then you have code to work with it.

Link to comment
Share on other sites

I have to agree strongly with requinix here.  There are many ways to format files to include configuration data that have support libraries and functions allowing you to read & write and convert them into data structures the language understands.  The ones I typically see are .ini, yaml and xml. 

You also have the option of using a plain old php array and serialize().  

Since your desire is to then manipulate this data and output html via javascript code, as suggested, json is the single best practice which you see used just about everywhere for REST api's as it is the best way to get serialized data into javascript objects.

You could base your structure on a php array and use json_encode to convert it to json for output to your menu code, or just store it in json format on disk.  Either way you will be able to accomplish what you want in a standard way that is highly supported both by php and javascript.  

 

Here's some code to consider:

<?php

$menu = array ('Menu1' => array('sub item1' => 'subitem1.html', 'sub item2' => 'subitem2.html'), 'Menu2' => array('sub item3' => 'subitem3.html'), 'Menu3' => array() );

var_dump($menu);

echo json_encode($menu);

 

Outputs:

array(3) {
  ["Menu1"]=>
  array(2) {
    ["sub item1"]=>
    string(13) "subitem1.html"
    ["sub item2"]=>
    string(13) "subitem2.html"
  }
  ["Menu2"]=>
  array(1) {
    ["sub item3"]=>
    string(13) "subitem3.html"
  }
  ["Menu3"]=>
  array(0) {
  }
}
{"Menu1":{"sub item1":"subitem1.html","sub item2":"subitem2.html"},"Menu2":{"sub item3":"subitem3.html"},"Menu3":[]}

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.