Skip to content

Instantly share code, notes, and snippets.

@matchaxnb
Last active August 5, 2025 11:50
Show Gist options
  • Select an option

  • Save matchaxnb/c5dbfce8cc259c61dc67db08912bb4a6 to your computer and use it in GitHub Desktop.

Select an option

Save matchaxnb/c5dbfce8cc259c61dc67db08912bb4a6 to your computer and use it in GitHub Desktop.
hamac303's minimal template engine

Hamac303's minimal templating engine

This is barely a templating engine. It's a concatenator.

i don't run a processing webserver, i serve HTML pages.

This is the rendering engine. That's all there is to it.

Note: each entry in the template should be unique or it's bound to have bugs. Also entries should not be substrings of one another.

For example, if you create <!--md:example.md-a-cool-thing.md--> and <!--md:example.md--> you'll run into issues.

i don't care, i don't plan to name my files wrong.

<html>
<head><title>Example page</title></head>
<style type="text/css">
<!--css:inline.css-->
</style>
<body>
<!--md:intro.md-->
<div class="foo">some static contents in the template is possible too, really it's a HTML file</div>
<!--md:outro.md-->
</body></html>
.PHONY = (all)
all: ../index.html
../index.html: index.html.tpl *.md *.css
./templatize index.html.tpl ../index.html
#!/bin/bash
INFILE=$1
OUTFILE=$2
cp $INFILE outfile.new
grep -E -o '<!--md:.*-->' $INFILE | while read entry; do
tpl_file=${entry%-->}
tpl_file=${tpl_file#<!--md:}
tpl_file=$(echo $tpl_file | sed -e 's@[.][.]@@g' -e s@/@@g -e 's@[${}]@@g')
MKDN=$(markdown_py -o html -n $tpl_file)
OC="$(cat outfile.new)"
OC=${OC//"$entry"/"
$MKDN
"}
echo "$OC" > outfile.new
done
grep -E -o '<!--css:.*-->' $INFILE | while read entry; do
tpl_file=${entry%-->}
tpl_file=${tpl_file#<!--css:}
tpl_file=$(echo $tpl_file | sed -e 's@[.][.]@@g' -e s@/@@g -e 's@[${}]@@g')
MKDN=$(cat $tpl_file)
OC="$(cat outfile.new)"
OC=${OC//"$entry"/"
$MKDN
"}
echo "$OC" > outfile.new
done
cat outfile.new
cp outfile.new $OUTFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment