Home

Template-driven websites with m4 and make

When a website is driven by static and rarely changing content, it's very beneficial to have that page be entirely driven by html, avoiding messy databases, programming languages, and all of the problems that go with them.

requirements (all available on os x and any other unix)

m4

m4 is a macro processor that we will be using for substitution. The following is the entire <head> section of a website we want to share among many pages. Notice the "ARTICLE_TITLE" .


<!-- site_head.html -->
<head>
<title>ARTICLE_TITLE</title>
<style type="text/css">
div#ads {
        float: left;
        padding: 0em 3em 0em 0em;
}
</style>
</head>
</pre>

The next step is to tell our main page that want to use this template. I do this by beginning all of my pages with:

<html>
m4_include(site_head.html) 
<body<>

This tells m4 to substitute the given line for the referenced file.

make

make is a program that does things when files have been updated. The Makefile I use to generate a page looks like this:

all: htdocs/ htdocs/testpage.html

htdocs/ :
		mkdir -p htdocs
		
htdocs/testpage.html : site_head.html testpage.html
		m4 -P audioswfwithoutflash.html -DARTICLE_TITLE="A test page" > htdocs/testpage.html

This means any time site_head.html or testpage.html are modified, make will run the given m4 command (the -D is what makes the title substitution). It will also create the htdocs/ subdirectory if it doesn't exist. The 'all' target is the default and make try to make everything that is listed.

From here I just store these files in subversion (cvs also works), run make, and upload htdocs/* to my webserver.