|
Dynamic Generation Of Static Webpages
The Idea
The idea is simple. You develop your website at home, using all PHP features you like, such as inclusion of text files, extensive use of pre-defined and user-declared functions, classes, templates, and so on.
Once you are satisfied with your work, you could use your preferred browser to view all your pages, one-by-one, and save them in a directory of your hard disk, changing file extensions from .php4 to .html. But this approach would be quite time-consuming, expecially if you have several pages and need to re-generate them quite often.
Using lynx is a good alternative, since it can fetch the pages from the webserver and return them into the standard output. You could then write a batch procedure (a shell script) such as:
lynx -source http://localhost/dynpages/index.php4 > C:\Documents\StaticPages\mywebsite\index.html lynx -source http://localhost/dynpages/page2.php4 > C:\Documents\StaticPages\mywebsite\page2.html lynx -source http://localhost/dynpages/page3.php4 > C:\Documents\StaticPages\mywebsite\page3.html lynx -source http://localhost/dynpages/page4.php4 > C:\Documents\StaticPages\mywebsite\page4.html
The example here is for Windows, under other platforms you should obviously change the path after the > operator.
This way, when you want to regenerate all your pages, you just need to launch your batch file.
If you have a number of files, and/or their names change often, you could think about using PHP to generate the lines of your batch file:
<?php
define('SLASH', '\'); //* this is for use under Win9x;
//* under linux, use '/'.
define('CRLF', chr(13).chr(10));
$httpbase="http://localhost/samplesite"; //* local URL for your pages
$dyndir="E:/xitami/webpages/samplesite"; //* (do not use backslashes)
//* where your webserver keeps your pages
$staticdir="C:/windows/desktop/samplewebsite/generatedpages/online";
//* where you want your pages to be put
$parameters="?outputas=online"; //* parameters you might need
function changeslashes($path) {
return str_replace("/", SLASH, $path); //* useful under Windows
};
function subdir($path) {
global $dyndir;
if ($dyndir!=$path) {
return substr($path, strlen($dyndir)-strlen($path));
}
else {
return '';
};
};
function destination($path) {
global $staticdir;
$dest=$staticdir . subdir($path);
$dest=changeslashes($dest); //* useful under Win
return $dest;
};
function searchdir($dirname) {
global $httpbase, $parameters;
$handle=opendir($dirname);
while ($file=readdir($handle)) {
if ($file !='.' && $file !='..') {
if (is_dir($dirname.'/'.$file)) {
if (!is_dir(destination($dirname).'/'.$file)) {
echo "mkdir " . destination($dirname). SLASH . "$file" . CRLF;
}
searchdir($dirname.'/'.$file);
} else {
if (substr($file, -5)==".php4") {
$newname=substr($file, 0, strlen($file)-5).'.html';
echo "lynx -source $httpbase" . subdir($dirname) .
"/$file$parameters > " . destination($dirname) . SLASH . "$newname" . CRLF;
} else {
echo "copy " . changeslashes($dirname) .
SLASH. "$file " . destination($dirname) . SLASH. "$file" . CRLF;
}
}
}
closedir($handle);
};
searchdir($dyndir);
?>
The script is recursive, and will generate a batch file to generate all your PHP pages (making them normal HTML files) and to just copy all other files.
You might capture the output of this PHP script into a batch file and run it at once. For instance:
PHP -q generatepages.php4 > generatepages.bat generatepages.bat
This can be a batch procedure on its own, and so you need only a command to generate all the pages of your web site.
I wrote and tested this script under Windows. It should also work under Linux, provided that you change something here and there (you wouldn't need to change slashes to back slashes, you would use cp instead of copy, etc.).
As you may notice, you can encode in the URL address (the query string) one or more parameters, as usual. I often take advantage of this, using parameters to control which stylesheet to use, or which content to display on my pages.
To make this clear, I'll show you the values I give to the outputas parameter:
- online
to use when you generate static pages to be published at an internet site (links have to get .html extension, links to directories have no file name added) - offline
to use when you generate static pages to be viewed off line, with no webserver involved (links have to get .html extension, links to directories have to be added index.html) - print
to use when you want to print the pages you are generating (I choose a different stylesheet, and write remote URLS in brackets after linked text)
Since the value of parameters is available for all the functions you call in your webpage, you can make good use of this.
[ Next Page ]
| Comments: | ||
| Problem posting forms | Olanrewaju, Michael Oluseyi | 02/28/05 14:25 |
| about php | gaya | 02/16/05 03:08 |
| The best php site copier | Leonardo Forero | 02/28/03 22:34 |
| RE: dynamic/static web sites? | china_boy | 12/30/02 04:18 |
| Need some advice please | Dave McKergan | 05/09/02 09:04 |
| RE: Use pavuk wget plus more | James Wilford | 04/09/02 21:30 |
| RE: dynamic/static web sites? | Ellen Richardson | 02/27/02 12:06 |
| what if you don't have linux | Edith | 12/06/01 09:13 |
| dynamic/static web sites? | John | 11/30/01 17:28 |
| RE: wget to the rescue | What if the site has passwords ? | 11/26/01 16:01 |
| Url argument and javascript is solution | rachmadji wibowo Ph.D | 11/02/01 04:40 |
| RE: What about URL arguments??-a solution | Vladimir | 10/04/01 11:08 |
| static and dynamic | david | 10/03/01 16:14 |
| letter | suhas | 09/15/01 10:48 |
| Use pavuk wget plus more | A K | 08/22/01 06:39 |
| RE: Frontpage | Neil | 08/15/01 23:29 |
| RE: What about URL arguments?? | Stephen Webb | 08/08/01 23:26 |
| a different approach: Speeding up page loads | Alfredo Rahn | 07/31/01 05:55 |
| IP Address configuration | Saravanan | 07/31/01 02:37 |
| RE: If you have mod_php you can use long URL's | Russel | 07/29/01 00:03 |
| RE: If you have mod_php you can use long URL's | Russel | 07/28/01 23:56 |
| RE: why re-invent the wheel? | Raven | 07/11/01 09:52 |
| RE: No need for Lynx, use fopen(http://) | Bernhard Dreyer | 06/12/01 10:15 |
| just run php directly man | Radek Burkat | 06/11/01 12:10 |
| If you have mod_php you can use long URL's | Bill Peck | 06/07/01 16:39 |
| RE: What about URL arguments?? | Rick | 05/25/01 07:52 |
| static or dynamic? | Ryan Thomas | 05/14/01 07:03 |
| RE: wget to the rescue | certron | 05/13/01 09:10 |
| RE: why re-invent the wheel? | lanson | 05/07/01 23:57 |
| Keep semi-static o r semi-dynamic | Tjin | 05/04/01 14:43 |
| if your html on a web cd use javascript!!! | Daniel | 05/02/01 15:24 |
| why re-invent the wheel? | foo | 05/02/01 07:57 |
| Does anybody have a simple example? | Ali Driver | 05/01/01 00:44 |
| RE: wget to the rescue | Stefan Waidele | 04/30/01 21:29 |
| RE: What about URL arguments?? | Jonathan Melhuish | 04/30/01 15:32 |
| Frontpage | Teo Danardi | 04/27/01 16:09 |
| RE: What about URL arguments?? | Loris Tissino | 04/27/01 00:52 |
| RE: keep it simple | Loris Tissino | 04/27/01 00:48 |
| RE: wget to the rescue | Loris Tissino | 04/27/01 00:46 |
| RE: just use the php binary! | Loris Tissino | 04/27/01 00:45 |
| RE: What about URL arguments?? | Thomas Donadt | 04/26/01 16:18 |
| RE: What about URL arguments?? | Andreas Bernhardsen | 04/25/01 10:36 |
| RE: wget to the rescue | Ali Driver | 04/25/01 07:26 |
| RE: just use the php binary! | DrTebi | 04/25/01 05:12 |
| RE: wget to the rescue | eric | 04/24/01 14:17 |
| RE: wget to the rescue | dave | 04/24/01 00:49 |
| RE: What about URL arguments?? | Thomas Donadt | 04/23/01 16:04 |
| just use the php binary! | Larry | 04/23/01 14:38 |
| Good Idea...Webserver,.XML Dynamics,c++ | John | 04/22/01 07:35 |
| No need for Lynx, use fopen(http://) | John Lim | 04/22/01 01:05 |
| I don't exactly get it | Miguel Cruz | 04/21/01 23:35 |
| What about URL arguments?? | Andy Dickinson | 04/21/01 19:29 |
| keep it simple | Tom | 04/21/01 12:57 |
| Lynx vs. other tools | Stralle | 04/20/01 20:26 |
| wget to the rescue | Scott Marlowe | 04/20/01 17:18 |
|
If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly. | ||


