CTemplate is a simple but powerful, extremely fast HTML template system for C language. It provides separation between code and presentation.
High Performance. CT precompile templates into object code at compilation stage, not at runtime. That means no loading and interpretation overhead. Side effect: template verification, you could not get incorrect template during runtime
C is used as template language. Anyway templates has been invented for logic/presentation separation and code should not be placed in template. And when some code is really necessary in template C can be used.
Does not enforce data model. C variables can be directly used in template. That means no overhead on data model translation
Simple and compact. No lost and misspelled template files, no access restrictions, no extra files at all, zero administration cost. No extra template language, No bloat of any of Database functions, Date manipulation, Macros etc.
There is a lot of tempate systems around. Most of them are implemented in and intended for using with higher level languages like php, python, perl, java. But sometimes the best solution to problem is to embed functionality into http server itself as apache module or CGI script. The simplest way to output something from apache module is to use ap_rprintf. Yeah, that is not easy. D. Richard Hipp invented a preprocessor for his CVStrac called translator. Now you can embed HTML into your code. Much better, but the result still have low maintainability.
Template paradigm comes to help. The idea of templating is well described here, here and here.
Lets take a look at some implementations. Most of them are suffered from several common drawbacks:
They have own template language. developers and designers have to study one more language.
Usually they are interpreted templates (slow), sometimes with intermediate bytecode caching(overcomplication).
Mostly they force developer to use some form of special data formats.
It's better to see once. Lets modify the mod_example from apache distribution. We are starting with copying the original mod_example.c into mod_tplexample.c
open mod_tplexample.c in your favorite editor and search for a bunch of ap_r* function calls. Cut it into example.htpl then
edit it by removing all ap_rputs and converting ap_rprintf's. Here is a stage 1, stage 2, stage 3, which is basically HTML with variables: stage3 as HTML.
Now we should describe the data model which is basically a C function arguments. We add it into the header section of the template.
Some parts are still missing: x_cfg type description and TRACE_NOTE which are in mod_tplexample.c file. Extract it into mod_tplexample.h. Include it into our template and the module source. Replace all ap_rputs* with template call.
h2ap example.htpl
apxs -c -I. mod_tplexample mod_tplexample.c example.c
CTemplate is available for free, at no charge, and it is released under BSD license.
Use SourceForge project download page to download most recent release.