Welcome to the C++ CTemplate system! (This project was originally called Google Templates, due to its origin as the template system used for Google search result pages, but now has a more general name matching its community-owned nature.)
As a quick start, here's a small but complete program that uses this template library. For more details see, the links below.
example.tpl
Hello {{NAME}},
You have just won ${{VALUE}}!
{{#IN_CA}}Well, ${{TAXED_VALUE}}, after taxes.{{/IN_CA}}
example.cc
#include <stdlib.h>
#include <string>
#include <iostream>
#include <ctemplate/template.h>
int main(int argc, char** argv) {
ctemplate::TemplateDictionary dict("example");
dict.SetValue("NAME", "John Smith");
int winnings = rand() % 100000;
dict.SetIntValue("VALUE", winnings);
dict.SetFormattedValue("TAXED_VALUE", "%.2f", winnings * 0.83);
// For now, assume everyone lives in CA.
// (Try running the program with a 0 here instead!)
if (1) {
dict.ShowSection("IN_CA");
}
std::string output;
ctemplate::ExpandTemplate("example.tpl", ctemplate::DO_NOT_STRIP, &dict, &output);
std::cout << output;
return 0;
}
gcc -o example example.cc -lctemplate_nothreads
I can use the "nothreads" library because example.cc
doesn't use threads. If example.cc were threaded, I
would do something like this instead:
gcc -o example example.cc -lctemplate -pthread
See the README for more details about the two different ctemplate libraries.