您的位置:首页 > 其它

ctemplate简单使用test

2015-07-06 21:09 423 查看

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

欢迎提出建议或意见,谢谢

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: