Building an HTML Templating Tool

June 15, 2017

I've been maintaining this website pretty much all by hand. It can get pretty tiring and annoying after a bit, especially when I need to make changes across every single page (relax, I'm not in-lining CSS, just making some layout changes in HTML). This got tiring after a while, and I wanted a way to make the task less burdensome. While I'm sure there are services out there that allow you to build multiple web pages from a standard layout, I figured writing my own wouldn't be too difficult and would be a good way to keep my programming skills fresh. Thus, HTemplate was born.

In addition to keeping my coding skills sharp, I also wanted to use this project as an opportunity to learn a bit more about Apache Maven, an incredibly popular build-automation tool for Java (and some other languages too). More specifically, I wanted to work with the testing feature provided by Maven. I've never written unit tests of any kind before, so if you decided to look through my code you'll probably find that my unit tests are rather lacking. Hopefully that won't be the case next time.

Right from the get-go, I knew that I wanted to use a marker system for this project, i.e. I'd have specific marking text in the template marking where custom HTML was supposed to go, and marking text in each of the content files to match up custom HTML with the template. Thankfully Java provides a great method to search within strings, the indexOf() method. Using this, I would simply find the locations of each marker in the template and then replace it with the corresponding custom HTML in a content file.

Unit testing on this project turned out to be incredibly helpful, simply because I didn't have to type out long file paths everytime I wanted to test my program. Hopefully I'll get to explore some of the more powerful uses of unit tests in the near future.

Because of the way I setup my file detection, the original version only worked if all your content files were in the same directory. Nested folders would simply be skipped over. I thought that implementing support for this would be difficult, but it turned out to be as simple as recursively calling the function that did all the heavy lifting, but with the subfolder instead of the root directory. Now that this project is actually somewhat useful, I'm going to begin the task of migrating this website over so that I can use HTemplate to maintain it.

If you'd like to use HTemplate for your own site or simply just check it out, click here.