Read my book

I wrote books about Webpack and React. Check them out!

Friday, May 27, 2011

Using PHP to Generate Static Websites

This time I'll show you how to use a bit of PHP to generate static websites based on given templates. The cool thing about this kind of approach is that you may use features provided by your templating engine (inheritance, variables, whatnot) while not depending on PHP at the server side.

It is easy to translate this idea to other languages. I'll just stick to PHP in this post since that's what I ended up using. In case you need something more advanced, you might want to check out a pre-made solution such as Jekyll.

Problem


So why did I end up writing a static website generator? By a chance really. I started out writing a regular PHP based site. In this case templates are evaluated and cached by the server. As I ended having problems with the server I was using (old sw) I decided to try something different. Why not to keep the server side hassle at minimum and just pass some good ol' HTML to it?

I am aware this won't quite scale for all purposes. In case you don't need to deal with databases and such, this might do the trick for you.

Solution


In this case I'll use Dwoo for templating and a bit of PHP to glue things together. If you have used Smarty, you'll probably find Dwoo easy to pick up. I'm sure other templating engines will work out just fine with some hacking. These days I might be more inclined to use HAML, SASS and such but Dwoo fit the bill in this time just fine.

The following snippet contains the script I ended up using. It's not the tidiest solution around but it did the trick for me. I really don't like PHP APIs for some reason...

It just fetches your templates from predefined directory, compiles them and outputs them to given directory. In addition it copies some files (CSS, images) around. Once the build is done all you need to do is to upload the output to your site and enjoy.

Since I like to use a hierarchical structure for defining my sites, I ended up doing a little hack to make it easier to make links between them. The script calculates "pathPrefix" variable that may be injected to a template. It contains path prefix relative to the root making it easy to refer there in your links.

Conclusion


Sometimes it's easier to work around problems by skipping using some technology altogether. No, technology is not the solution always. :)

If you dig around bit, you'll probably find a lot related resources. I just came by this nice list containing whopping 32 generators. Perhaps I shouldn't have written my own after all... Oh well. :)