google ctemplate——c++模板引擎
2017-09-19 14:27
507 查看
1 概述
在进行web开发时,使用传统的CGI方式,在C/C++程序里面既要处理逻辑,也要处理页面显示内容,会比较混乱。可以通过模板引擎,使得逻辑与显示的分离。Google CTemplate就是其中一个开源的C++模板引擎。使用ctemplate不仅可以产生html,还可以生成xml,json等格式的内容。源码地址:https://github.com/OlafvdSpek/ctemplate
2 示例
2.1 模板文件
<html> <head> <title>ctemplate示例模板</title> </head> <body> {{table1_name}} <table> {{#TABLE1}} <tr> <td>{{field1}}</td> <td>{{field2}}</td> <td>{{field3}}</td> </tr> {{/TABLE1}} </table> </body> </html>
2.2 C++端代码
#include <stdio.h> #include <string> #include <dlfcn.h> #include <ctemplate/template.h> 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.html", ctemplate::DO_NOT_STRIP); tpl->Expand(&output, &dict); printf("%s\n", output.c_str()); return 0; }
3.3 运行输出页面内容
root@qwl-desktop:~/ctemplate-test# ./example <html> <head> <title>ctemplate示例模板</title> </head> <body> example <table> <tr> <td>1</td> <td>2</td> <td>0</td> </tr> <tr> <td>1</td> <td>2</td> <td>1</td> </tr> </table> </body> </html>
相关文章推荐
- C++ 模板引擎 Ctemplate 的介绍
- C++模板引擎Ctemplate的介绍
- C++模板引擎Ctemplate的介绍
- C++ Template 基础篇(二):类模板
- 【模板】c++ template模板实现string到Num的转换
- C++中 模板Template的使用
- 玄宇说:简单的VTemplate模板引擎的使用
- C++ 函数模板(十四)--template 泛型函数模板、通用函数、重载模板
- 有关C++模板(template)的编译错误“error LNK2019: 无法解析的外部符号”的分析
- javascript模板引擎 Arttemplate
- template.js——前端模板引擎
- mctemplate大型WEB应用程序的模板引擎
- TinyTemplate模板引擎火热出炉,正式开源了
- JS模板引擎-腾讯artTemplate 最全的使用介绍
- C++学习笔记36 (模板的细节明确template specialization)和显式实例(template instantiation)
- C++基础::变量模板(variable template)
- 【C++模板】特化与偏特化 template [partial] specialization
- nodejs基于art-template模板引擎生成
- C++中 模板Template的使用
- C++中 模板Template的使用