Lairs Johnstone

Autosite: web tools in Go

Autosite is what I call the simple tools I wrote in Go in order to implement this website and blog. This page describes my motivations, and an overview of how the framework — even though it feels too small for that word — can be used.

Objectives

I mainly wanted the following from my web tools:

An example

When I wanted to write the page you're reading now, I created a new file called pages/autosite.tmpl, with the following structure:

{{define "page_title"}}
Page title goes here.
{{define "end"}}

{{define "page_content"}}
Page contents (which can include any HTML) goes here.
{{define "end"}}

That's all — the staging version of my site now started serving http://[host]/autosite, showing the output every time I saved the .tmpl in my editor and reloaded the page. Once the live version of my site was pushed, it now started serving this page at http://www.hkjn.me/autosite.

Structure

Backing up a bit, the reason a simple .tmpl file written in the syntax of Go's html/template produced a HTML page to be served was due to the following bit of config that told the autosite framework where to look:

var baseTemplates = []string{
	"tmpl/base.tmpl",
	"tmpl/base_header.tmpl",
	"tmpl/style.tmpl",
	"tmpl/fonts.tmpl",
}

var web = autosite.New(
	"Lairs Johnstone",
	"pages/*.tmpl", // glob for pages
	"www.hkjn.me",  // live domain
	append(baseTemplates, "tmpl/page.tmpl"),
)

The framework instantiates the base template, but apart from that is agnostic to the structure of your pages — as long as you provide the .tmpl files needed to compile the pages, you may structure them any way you desire.

In my case, as you may have surmised, I try to be minimalistic. Here is the entirety of my base.tmpl:
{{/* base.tmpl; base template that everything else depends on */}}
{{define "base"}}
<!DOCTYPE html>
<html>
<head>
  {{template "head" .Title}}
</head>
<body>
  {{template "base_header" .}}
  {{template "main" .}}
  {{template "js" .}}
</body>
</html>
{{end}}

The main template invoked by base.tmpl above is defined by two templates so far on my sites — blog.tmpl, and page.tmpl:

{{/* page.tmpl; a simple article template */}}
{{define "main"}}
<article>
<header>
<h1 class="article_title"><a href="{{.URI}}">
  {{template "page_title" .}}
</a></h1>
</header>
  {{template "page_content ."}}
</article>
{{end}}

Reusing most of the structure between the blog and the main website is a nice plus here, but of course the two domains could host completely different websites as well — as long as what's being served fits the pattern of minimalistic static pages like this, it works.

This was not a complete example — CSS and some other less interesting bits was not included. However, the main parts that define the sites are all here. I also did not discuss the actual implementation of autosite, as opposed to how it's used, but even taken together with the template hierarchy the entire thing is fairly compact:

$ wc -l {hkjnweb,autosite}/*.go hkjnweb/tmpl/*.tmpl
  248 autosite/autosite.go
  141 autosite/blog.go
   46 hkjnweb/hkjn.go
   14 hkjnweb/tmpl/base.tmpl
   13 hkjnweb/tmpl/base_header.tmpl
   14 hkjnweb/tmpl/blog.tmpl
   15 hkjnweb/tmpl/blog_listing.tmpl
   58 hkjnweb/tmpl/fonts.tmpl
   13 hkjnweb/tmpl/head.tmpl
    3 hkjnweb/tmpl/js.tmpl
   11 hkjnweb/tmpl/page.tmpl
   77 hkjnweb/tmpl/style.tmpl
  653 total

Source

I'm sharing the code under the MIT License in the hope that others may find it useful. Please contact me if you make use of the framework in its current shape, fork it, or have any suggestions!