PDA

View Full Version : referencing code in a different file?



jennifergwl
06-05-2010, 11:29 PM
Ok, so I am using this menu:

http://www.dynamicdrive.com/style/csslibrary/item/jquery_multi_level_css_menu_2/

It's great! However, I need to use it on 50 pages. I don't want to have to edit 50 pages every time I need to make a change to the menu. How can I avoid that and create one file with all the info and reference it in the 50 pages and just change the one? Hope I worded this correctly. Any questions or help, please let me know! :)

Jenn

djr33
06-06-2010, 03:08 AM
Can you use PHP on your server? It's very easy and will be seamless. There may be ways around it with Javascript, but using PHP is more reliable and probably easier, if you can use it. Other similar options include SSI, ASP, etc, if you have those instead.

If you do have PHP, all you need to do is:
1. Convert all of your pages to .php -- this will make your server process them as PHP code [which generates HTML-- a .html file renamed .php will be identical, it just allows PHP also].*
2. Create a new file called anything you like, let's say menu.php.
3. In each page you want the menu, just use this PHP code:
<?php include('menu.php'); ?>
(Note that you may need to adjust the path to be relative to the current page.)


*I know this sounds like a lot of work, but it'll help in the end and PHP can do more automation processes, like moving more than just the menu to a central file. That way you can edit/maintain many pages at once.
If you [i]really want to avoid renaming your pages to .php you can configure your server (through .htaccess probably) to parse .html/.htm files as PHP as well, but this is bad for two reasons: 1) it may be a security risk: it's not dangerous to let people upload html files to your server, but if they parse as PHP it would be-- so if you have any uploads of this sort, it's not a good idea. That's probably not the case, though. And 2) this just means your server will overall work a little harder, parsing every page as PHP even if it is just a regular html page. But if all of your pages are going to have the menu anyway, it won't be a problem. If you want more advice on this issue, explain the general plans for your site.

jennifergwl
06-08-2010, 12:02 AM
Ok so, do I have to rename ALL pages on the site to php or just the ones using this particular menu?

bluewalrus
06-08-2010, 12:09 AM
You'll either need to change the pages to php or make the server read the extension they have as if they are php.

http://php.about.com/od/advancedphp/p/html_php.htm

djr33
06-08-2010, 12:33 AM
You don't need to rename every single file on the site, just those that you are going to use PHP code in. You can have both htm and php files, but php will only work in the .php files. However, I'd suggest renaming ALL of them, so that you keep things consistent and can easily add the menu (or another menu, etc) to other pages later if you want, rather than splitting things up in a hard-to-remember way. But, sure, it's fine if you just have some php pages with this menu and other unrelated .htm files.

jennifergwl
06-08-2010, 08:03 PM
Well, the pages I need to use the .php are in a password protected folder and will only be used on those pages, so it actually won't be confusing. However, I am using another menu for other pages on the site that I just might change over eventually anyway down the road since it will make life soooo much easier! Thanks so much!

djr33
06-08-2010, 08:49 PM
Ok, glad that works. Before you rename all of the files, be sure that you do have PHP installed. I mentioned that above, but you didn't confirm it is on your server. If you go through all of that effort then find you can't use PHP, it will be frustrating. Most hosts do have it, though, so it shouldn't be a problem.

jennifergwl
06-10-2010, 08:41 PM
Oh yes sorry, we do have that on our servers. It's worked out very well!! :)