ctemplate 模板替换库的简单使用实践
其中遇到了很多问题。
* 编译时动态链接库没有加入,导致报错
* tpl文件内容出现偏差
下面将代码呈现出来
exmaple.tpl
{{NAME}} welcome,
congratulations, your bounty${{VALUE}}!
{{#IN_CA}}your tax is: ${{TAXED_VALUE}}. {{/IN_CA}}
test.cpp
#include <iostream>
#include <stdlib.h>
#include <string>
#include <ctemplate/template.h>
using namespace std;
int main(void)
{
ctemplate::TemplateDictionary dict("example");
dict.SetValue("NAME", "John Smith");
int winnings = rand()%10000;
dict.SetIntValue("VALUE", winnings);
dict.SetFormattedValue("TAXED_VALUE", "%.2f", winnings*0.83);
dict.ShowSection("IN_CA");
ctemplate::Template *tpl =template::Template::GetTemplate("example.tpl",template::DO_NOT_STRIP);
string output;
tpl->Expand(&output, &dict);
cout<<output;
return 0;
}
编译命令
g++ -g -o test test.cpp /usr/local/lib/libctemplate_nothreads.a