During the past two months I have been working on a new project for a local little company. One of the parts of this projects is to create a simple website.
On this point I started to look for a static site generator on google and I found staticgen, I tried some of them but no one fit on my need, so I decided to create my own. A lightweight and simple static site generator, that allows me feel comfortable during the development but doesn’t force me to use a set of templates or a determinate folder structure and if one day I need to add some backend framework all my work could be easily adapted as templates.
After read the Mustache.js documentation I decide to use the Command Line Tool to generate the html files. I am going to skip the explanation about the npm dependencies and package.json setup and lets start at this point with the package.json and the basic files. As dependencies we have Mustache, npm-watch and buildify, although some of them seem useless now, they will have an explanation on next paragraph. Now we have two scripts generate.js and generate-command.js and two folders tpl and generable-pages. This dependencies and structure is the process of the development but I am skipping the steps that lead me to this, to focus just on the result.
In case you don’t know how mustache works, there are two parts, the logic-less template which contains the structure with the tags:
And in the other hand the view which represent the data model for the template:
The way I have chosen is to create a “target” template which represent a base file where I include my partials, the partials are mustache template with a common content for many pages or with a business representative part, for instance, I have a partial for navigation bar because it is same for every page and a partial for the budget calculator because it is important for my business, so the index.mustache will look like this:
For generating the “index.html” with the mustache command tool it is necessary a command like this:
The first step to create this command it is to relate the target template and the partials, for that, we use “generable-page” folder, I have created a file that contains this model:
A model has a target template, the partials and the view. To get the command with the generable page model, the command-generator.js that allows us to compose the command that generate.js executes.
One of the requirements of Mustache cli is a json file that contains the view, that was a problem because I have common view elements on different templates, so creating json file for each page will duplicate a lot of content on the view files.
At this point I found buildify, this tool allows me to set a base template and then insert a list of files as the template body. I just have to create a json file for each part of the page and then join them all on a single file before the command is execute. Thats why i specify a name and a few partials in the generation model.
After all, to generate the html files that you have declared on generable-pages files you just have to run this:
This use npm scripts to run the generate.js script.
A generable-page file contains the generation model: a target, partials for target, the view name, partials for view and the output file.
The tpl folder contains the target templates, like index.mustache and his partials. The structure inside this folder is not important.
With this, I can create any kind of static sites, without themes limits, not oriented to any kind of project (like blog or landing pages), and the result can be easily adapt to a non-static server render project without to much effort.
I hope you like this project, it will be a pleasure for me to hear any feedback about this and answer any question.
You can find a getting started set up here