前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Google开源html模板库ctemplate的完整使用示例

Google开源html模板库ctemplate的完整使用示例

作者头像
一见
发布2018-08-07 16:54:37
2.1K0
发布2018-08-07 16:54:37
举报
文章被收录于专栏:蓝天

ctemplate是Google开源的一个C++版本html模板替换库。有了它,在C++代码中操作html模板是一件非常简单和高效的事。通过本文,即可掌握对它的简单使用。

示例html模板文件example.htm内容如下:

ctemplate示例模板

    {{table1_name}}

        {{#TABLE1}}

        {{/TABLE1}}

{{field1}} {{field2}} {{field3}}

模板中的变量使用{{}}括起来,

而{{#TABLE1}}和{{/TABLE1}}表示一个循环。

C++代码x.cpp文件内容如下:

代码语言:javascript
复制
#include 
#include 
#include 
int main()
{
    ctemplate::TemplateDictionary dict("example");
    dict.SetValue("table1_name", "example");
    // 为节省篇幅,这里只循环一次
    for (int i=0; i<2; ++i)
    {
        ctemplate::TemplateDictionary* table1_dict;
        table1_dict = dict.AddSectionDictionary("TABLE1");
        table1_dict->SetValue("field1", "1");
        table1_dict->SetValue("field2", "2");
        // 这里有点类似于printf
        table1_dict->SetFormattedValue("field3", "%d", i);
    }
    std::string output;
    ctemplate::Template* tpl;
    tpl = ctemplate::Template::GetTemplate("example.htm", ctemplate::DO_NOT_STRIP);
    tpl->Expand(&output, &dict);
    printf("%s\n", output.c_str());
    return 0;
}

编译:

g++ -g -o x x.cpp ./lib/libctemplate_nothreads.a -I./include

执行x输出内容如下:

ctemplate示例模板

    example

120

121

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012/08/15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档