home blog reviews contact

2020-04-02

Using the C Preprocessor to Template HTML (or: Why I'm Not a Web Developer)

I'm not a huge fan of web development. I find it fairly uninteresting to move boxes around by a few pixels. I'm not particularly good at design either (in case this website didn't give it away).

So as a big "fuck you" to all my web dev friends, I've used 0 JavaScript on this website, and, to make matters worse, I've templated it using the C Preprocessor (cpp for short).

What Do We Want From an HTML Templater?

For my purposes, I need the very minimum. Basically, I just want to avoid copy-pasting the same shit all over my website. Simple text substitution is all I need.

(You'd sure hope that's all I need based on how my website looks.)

What is cpp?

cpp is the preprocessor for C and C++ (in case the name didn't give it away). It's the first step of compiling a C or C++ program. Basically, anything with # is handled by cpp. cpp copy-pastes any files you included with #include, and expands any macros you defined with #define. (I'll get into details later, don't worry.)

cpp does very basic text substitution, and is almost completely ignorant of C and C++ syntax, save for knowing that quotes should be closed. (Which is actually a massive pain in the ass cause I use apostrophes sometimes.) It's kind of suitable for templating anything I guess, as long as you're willing to treat that anything as random meaningless text. (Which is exactly what I've done for my website.)

cpp can also be invoked on its own, without invoking the compiler proper, which is what I've done here. If you're using gcc, you can do this with gcc -E filename. I'm fairly sure all C or C++ compilers support something similar.

Why I'm Not a Web Developer

The nav bar at the top of every page uses the exact same HTML (as it should). Why bother copy-pasting that onto each page? Just #include "nav-bar.shtml" and we're done. (This is basically what Server Side Includes are, FYI. Also notice that I didn't even bother using the .shtml extension for the right thing.)

The date of a blog post shows up multiple times each post. Why bother writing it multiple times? Just do #define TODAY 2020-04-02 at the top of the file. Perfection.

Links to each of my blog posts on my blog are all basically the same format. The only thing that varies is the date and title. No problem. Just a quick macro (using some # and ## fuckery) and we're ready to go.

Basically, anything that needs to show up more than once can just be either moved to another file and pasted in, or defined as a macro. It's awful but it works. Honestly for what my website is, I don't think using a proper tool would even change much.

Conclusion

In conclusion, you don't need a real HTML templating language if you don't mind your website looking like shit.

wEb DeVELopMenT iS mY PaSSiOn.