I finally got back to the little website project that I've been
putting off finishing for about six months. I decided to do it as a
static site generator, formatting data which a shell script I wrote
collects into a directory tree.
If I searched online I probably would have found existing examples,
but I preferred to go via the PHP manual and work out my own method
from the bottom up. Here therefore is my own very short guide.
Capturing the output normally sent to stdout is done with
ob_start() and ob_get_clean(), so you just wrap them around an
"include" directive with the PHP code for a page, and then save the
captured text string to a file:
Here the directories above need indexes too, which is where it
keeps going wrong for me as now the templates have to be broken up
into "head" "entry" and "foot" sections, and then I decided to do
pagination again since there are thousands of entries. Last time I
tried splitting indexes into separate pages was when I wrote
another static site generator in Bash, and I got completely
confused. Switching to PHP doesn't seem to have helped. It formats
and indexes one single data file/directory very nicely. :)
Keeping the HTML bits in separate 'included' files does keep things
much tidier than usual, so that's probably a good technique for
dynamic sites as well. Maybe it'd be even tidier if there were some
way to designate "head" "entry" and "foot" sections in the same
file to include individually instead of mentally assembling them
while editing separate files for each, but I guess not. This also
allows very flexible theming as shown. But in my case it'll almost
certainly only ever have one, very bare-bones, theme anyway. I'm
not sure if the website will even be used by anyone but myself.
The one dynamic page to do when I finish the static stuff will be a
basic search page. Seems like something PHP would be pretty well
equipped for stand-alone, but everything points to big complicated
libraries: Lucene, Xapian. I think I'll roll my own with some sort
of search through a CSV file reminiscent of (or possibly calling)
Grep. It doesn't need to be all that good. I guess such libraries
are yet another thing I'd have to investigate seriously for my "big
website idea" though.