CTemplate

What?

CTemplate is a simple but powerful, extremely fast HTML template system for C language. It provides separation between code and presentation.

Why?

Why one more?

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:

How it works?

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

  1. open mod_tplexample.c in your favorite editor and search for a bunch of ap_r* function calls. Cut it into example.htpl then

  2. 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.

  3. Now we should describe the data model which is basically a C function arguments. We add it into the header section of the template.

  4. 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.

  5. Everything is ready for compiling:

    h2ap example.htpl

    Note the two generated files:
  6. apxs -c -I. mod_tplexample mod_tplexample.c example.c

  7. Enjoy (note: this is not an actual run, there is no way to load module into SourceForge's apache)

How much does CTemplate cost?

CTemplate is available for free, at no charge, and it is released under BSD license.

Where?

Use SourceForge project download page to download most recent release.

TODO

SourceForge.net Logo