PHPBuilder - Building Static Sites Page 3



RSS Twitter
Articles Html

Building Static Sites - Page 3

by: Loris Tissino
|
April 20, 2001

Something About Links And Relative Paths

While you develop your website, you will normally use the extension .php3 or .php4 for your files. If you write the code for a link straight in your HTML code, then you would have broken links when you generate .html files (since the name of the files have changed).
As a workaround for this, I normally use this trick: instead of writing the normal code for a link
<A HREF="page2link.php4">linked text</A>
I call a PHP function
<?PHP makelink("page2link", "linked text"); ?>
which, by default, outputs exactly the same HTML code as above, but that could output something different when given specific parameters in the query string.

<?php

function makelink($linkedtext$linkhref$kind=0$fragment=""$title=""$target="_top") {
   
//*$kind values: 0 means internal; 1 means external;

      
global $outputas;

      
$class = ($kind==0)? 'internal' 'external';

      if (
$kind==0) {
          
          if (
substr($linkhref, -1)=='/') {
               if (
$outputas=='offline') {
                   
$linkhref .= "index.html";  //* you can't have links to dirs when offline
                   
}
               }
          else {
               if (
$outputas=='online' || $outputas=='offline') {
                    
$linkhref.='.html';
                    }
               else {
                    
$linkhref.='.php4';
                    }
               };
          };

      echo 
"<A HREF=\"$linkhref$fragment\" CLASS=\"$class\" TITLE=\"$title\" TARGET=\"$target\">";
      echo 
"$linkedtext</A>";

      if (
$outputas=='print' && $kind==1) {
               echo 
" <SMALL>[$linkhref]</SMALL>";
               };  
//* for printed things, I want to write URLs in brackets after links
      
};

?>
Another little thing. Since your generated files could be in different directories, you could need to differentiate the path for some resources (for instance, the stylesheet, or the home page).
For this things, you can guess at which level is your page in the hierarchy of directories, and then produce a relative path to be used (for instance, "../../") when needed.
You'll just have to write this on the top of your pages:

<?php

  $level
=sizeof(split('/'$PHP_SELF))-3;
  
$rp $level>str_repeat("../"$level) : './';
?>
I enclose a zip file which contains a sample website in its PHP form, and in three differente generated versions.
-- Loris

« Previous Page
1
|
2
|
3

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Loris Tissino

Comment:



Comment:

(Maximum characters: 1200). You have characters left.