Raw File
php.php
<?php top("Using PHP"); ?>

    <p>
      Ok, this is not exactly related, but some has asked me how I
      "change" the content without losing the menus. Well, the way
      to do is it by HTML templating it in some way, for instance using PHP.
      So here is how I do these pages using PHP.
    </p>
    <h2>Auto including</h2>
    <p>
      There are two ways to do this, one of them is having a template
      file with <code>${content}</code> or some other text that your
      templating system is going to replace with your actual content.
      The other way is to just split the file into a "top" and "bottom".
      This is what I've done on these pages. I have a top.php with
      everything up to the <code>&lt;div class="content"&gt;</code> tag,
      including the CSS imports and left menu, and then a bottom.php
      with the closing div tag and the bottom "frame".
    </p>
    <p>
      One nice way to do this with PHP is to do it automatically using
      local <code>.htaccess</code> statements. In a file
      <code>.htaccess</code>, add these lines:
    </p>
    <pre>
php_value auto_prepend_file "top.php"
php_value auto_append_file  "bottom.php"
php_value include_path "include"
Options +MultiViews
    </pre>
    <p>
       Keep top.php and bottom.php in a subdirectory
       <code>include</code> to avoid mixing up included files and "real"
       content. The files will be automatically included as if the PHP
       statement <code>&lt;?php include("top.php") ?&gt;</code> was
       present at top, and vice versa at bottom.
   </p>
   <h2>Nicer URLs</h2>
   <p>
       The MultiViews option let you access pages without
       including the <code>.php</code> ending, therefore giving nicer
       URLs. For each content page, name the file
       <code>something.php</code> and just begin your content with
       <code>&lt;p&gt;Hello&lt;/p&gt;</code>. In links, use just
       <code>something</code> as the relative URL. The filenames could
       actually also be used in your menu.
    </p>
    <h2>Auto generated menu</h2>
    <p>
       This is more like a hack, but a quick menu can actually be
       autogenerated. In your <code>include/top.php</code>, let your
       menu-"frame" look something like this:
   </p>
   <pre>
 &lt;div class="menu"&gt;
 &lt;?php
   global $SCRIPT_FILENAME;
   if (preg_match("/index.php$/", $SCRIPT_FILENAME)) {
     echo "&lt;a class='current' href='/frames_with_css/'&gt;Frontpage&lt;/a&gt;\n";
   } else {
     echo "&lt;a href='/frames_with_css/'&gt;Frontpage&lt;/a&gt;\n";
   }
   ?&gt;
&lt;?php include("menu.php"); ?&gt;
   &lt;a href="simple"&gt;Simple example&lt;/a&gt;
   &lt;a href="scroll"&gt;Scroll example&lt;/a&gt;
 &lt;/div&gt;
   </pre>
   <p>
     The nasty part here is trying to separate out the frontpage,
     setting the "current" flag to make it appear selected, and typing
     in the URL to it manually since we can't refer to it relatively (we
     want to avoid <code>index</code> appearing in the URL, right?)
   </p>       
   <p>
       And now for the <em>real</em> magic that resides in
       <code>include/menu.php</code>:
   </p>
   <pre>
&lt;?php
include_once("utils.php");
$url = "http://www.soiland.no/frames_with_css/"; 
?&gt;
&lt;?php
 global $SCRIPT_FILENAME;
 foreach(findfile('.', '/.php$/') as $file) {
     $file = preg_replace("/.php$/", "", $file);
     $file = preg_replace(",.*/,", "", $file);
     if ($file == "index") {
         continue;
     }
     $name = ucfirst($file);
     $name = preg_replace("/_/", " ", $name);
     if (preg_match("/$file/", $SCRIPT_FILENAME)) {
         echo "&lt;a class='current' href='$url$file'&gt;$name&lt;/a&gt;\n";
     } else {
         echo "&lt;a href='$url$file'&gt;$name&lt;/a&gt;\n";
     }
 }
 
?&gt;
   </pre>
   <p>
     This oh-so-fucking-ugly hack searches for all files named
     <code>*.php</code> in the current directory, and includes a link to
     that file in the menu. Some weird regexes checks if this is the
     active page and highlights it, while some other functions make up a
     <code>$name</code> that is a bit more readable to the menu than the
     filename, for instance "Simple example" instead of
     "simple_example". Note that the <code>findfile</code> function is
     something I made in <code>utils.php</code>. It's all included
     in the tar ball.
   </p>
   <h2>Titles</h2>
   <p>
     OK, I lied a bit before, because having just the HTML in the
     <code>include/top.php</code> will force you to have the same
     <code>&lt;title&gt;</code> and <code>&lt;h1&gt;</code> values for
     all pages - making Google hits and bookmarks look pretty silly. 
     So to be able to specify the title, what is really done in 
     my <code>top.php</code> is to specify a function <code>top()</code>
     that I call on the top of every .php file. This is dangerous, as
     leaving that function call out will render the page useless. 
   </p>
   <p>
     But this quick hack lets me specify the title as a parameter. So
     the first line of this file is  <code>
     &lt;?php top("Using PHP"); ?&gt;
     </code>.
   </p>
   <h2>Download</h2>
   <p>
     You might download a complete <a
     href="frames_with_css.tar.gz">tar-ball</a> of this whole PHP 
     hack for your own use or inspiration. I've not set a specified
     license, you might use this stuff for whatever you like. If you
     have any other questions, don't hestitate to 
     <a href="http://soiland.no/about">contact me</a>.
   </p>
back to top