Minimal DIY Static Site Generator that outputs PHP to static html-pages.
Main benefit of doing web-development like this is being able to have a simple, low-fi implementation of templating, here with just PHP. It can be appropriate for smaller websites with a size between three to twenty pages or so.
The bash script named io.sh
, is what makes it happen: curl
to output static HTML-files from PHP-files, which needs to be executed from the terminal.
#!/bin/bash -x
# write to file
write_to_file()
{
curl "http://localhost/~www/houtje-touwtje/php/index.php" > "../index.html"
curl "http://localhost/~www/houtje-touwtje/php/lab/index.php" > "../lab/index.html"
curl "http://localhost/~www/houtje-touwtje/php/about/index.php" > "../about/index.html"
curl "http://localhost/~www/houtje-touwtje/php/contact/index.php" > "../contact/index.html"
}
# execute it
write_to_file
Be sure though that the shell scripts have the right permissions set. On UNIX base systems you can do this with:
chmod +x io.sh
or
chmod 755 io.sh
Configuration
Rename the directory names in io.sh
to resemble the ones on your own machine. Each time one creates a new page, add that link to io.sh
, and note that the first path to the php files should be absolute url’s.
Also edit at least config.php
(php/inc
folder) and the other global variables on top of each page, to resemble your environment and preferences.
Also before generating for the last time - before uploading to the remote server, change the path to the root folder in config.php
, for otherwise the absolute paths to the assets are wrong. You can automate this a bit by executing a shell script for this, it simply renames config-external.php
to config.php
and likewise from config-local.php
. You can do it like this:
go to inc
folder
cd php/inc
execute shell script to change the root directory to the external server paths:
./external-config.sh
execute shell script to change the root directory back to the local development environment:
./local-config.sh
Generate
Now in the terminal navigate to the folder containing the php folder
cd ~/Sites/houtje-touwtje/php
type the following and hit Enter
:
./io.sh
Deployment
You will end up in this setup with a mirrored site; one static html site in your “houtje-touwtje”-root
-folder, one dynamic php site in the php
-folder. Presumably the static-files are the ones that going to be (FTP
) uploaded to the remote server. (Or with git-hub.com/gh-pages “pushed to origin”).
Final Thoughts
When just wanting to regenerate one page, an alternative way to do that in the terminal besides curl
, is with PHP CLI
:
php -f about/index.php > ../about/index.html
Two ways to repeat the previous command from the command line quickly:
- Use the up arrow to view the previous command and press
enter
to execute it. - Type
!!
and pressenter
When one doesn’t want the gzip-encoding that takes care of compression of the html-output, (for instance when using a lot of pre
tags) just move, or remove the ob_gzhandler.