Html – Offline HTML templating processor for Windows

htmltemplates

I am teaching my friend how to make websites. I would like to keep the websites static, so that he does not [yet] have to learn PHP, worry about testing on his Windows machine, worry about the server configuration, security etc. The bad thing is that without some tool support he would have to repeat a lot of code, for example the menu block on each page is almost the same. Is there a simple tool that would help him?

I’d like something that runs on Windows without too much work, ie. without Perl, Cygwin, IIS, PHP and such. (A simple GUI tool or a text editor with some special support for this would be nice.) I’d like something that does not require keeping separate “source” and “build” file versions, ie. the source files with some special markup and the build ones with regular HTML. (I hate to re-make the whole thing after each change in code.) I’d like something free and simple.

Is there such a thing?


Update: I was dreaming about something that would work like this:

$ cat page.html
<h1>A page</h1>
<!-- insert menu -->
<!-- menu ends -->
$ cat menu.tmpl
<ul id="#menu">…</ul>
$ update-templates page.html && cat page.html
<h1>A page</h1>
<!-- insert menu -->
<ul id="#menu">…</ul>
<!-- menu ends -->
$ echo "Moo" > menu.tmpl
$ update-templates page.html && cat page.html
<h1>A page</h1>
<!-- insert menu -->
Moo
<!-- menu ends -->

…only in GUI. BTW: Thanks for the JavaScript solutions. These did not occur to me, but the website has to work even with JS turned off.


Update: As I did not find any existing solution, I’ve written the script as a Google Code project. There are some quirks to be handled (like the different line endings on different systems), but the template substitution stuff works. The script requires Perl, but otherwise all you have to do is to double-click on an icon to get the HTML sources updated.

Best Solution

The easiest would probably be (although it requires a little bit of set up) a web server with server-side includes. It's the easiest form of templates that I can think of, and if you don't want to "build" the html files from some kind of source, then you need something which is actively serving requests.

I don't know if it's standardized, but Apaches SSI looks like this:

<!--#include file="menu.html" -->

Hope that helps.

EDIT:
I was convinced that not having separate source and target/build files it'd be impossible to achieve without an active server. However, as you commented about comment-markers a thought struck me, it shouldn't be to difficult to construct a simple perl-script that includes a file, replacing everything between a start-commment marker and an end-comment marker. You also said you don't want to mess with perl, did that include a prepared perl-script that he just needs to execute?

EDIT 2
A simple few-liner perl script using the /e-regexp modifier could be sufficient, since you're using windows I don't know if you can use backticks like cat $file, so I added a readfile sub.

sub readfile($) {
  open(FILE, 'r', shift);
  my $buffer;
  read (FILE, $buffer, 2 ** 20); # one megabyte maximum.
  close(FILE);
  return $buffer;
}
sub writefile($$) {
  open(FILE, 'w', shift);
  print FILE shift;
  close(FILE);
}

for my $file(@ARGV) {
  my $content = readfile($file);
  $content =~ s/\<!--\s+include\s+(\S+\)\s+--\>.*?\<!--\s+end\s+\1\s+--\>/"<!-- include $1 -->".readfile($1)."<!-- end $1 -->"/ge;
  writefile($file, $content);
}

Be wary though, that a crash or a bit greedy replacement (perhaps due to a typo) will kill the entire file, and this without maintaining a source file. I'm also unsure if LHS will match correctly with the backreference, I need to look into that.