Google开源html模板库ctemplate的完整使用示例
2012-08-15 21:05
531 查看
ctemplate是Google开源的一个C++版本html模板替换库。有了它,在C++代码中操作html模板是一件非常简单和高效的事。通过本文,即可掌握对它的简单使用。
示例html模板文件example.htm内容如下:
<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>
模板中的变量使用{{}}括起来,
而{{#TABLE1}}和{{/TABLE1}}表示一个循环。
C++代码x.cpp文件内容如下:
#include <ctemplate/template.h>
#include <stdio.h>
#include <string>
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输出内容如下:
<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>
示例html模板文件example.htm内容如下:
<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>
模板中的变量使用{{}}括起来,
而{{#TABLE1}}和{{/TABLE1}}表示一个循环。
C++代码x.cpp文件内容如下:
#include <ctemplate/template.h>
#include <stdio.h>
#include <string>
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输出内容如下:
<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>
相关文章推荐
- 使用eclipse构建Maven项目--web工程完整示例
- 【Android】使用AIDL传递用户自定义类型数据--附完整示例代码
- 如何使用WebBrowser控件打印格式化的XML文档,并以编程方式任意设置打印方向(C#完整示例)
- 通过完整示例来理解如何使用 epoll
- 使用secondary sort实现数据关联 完整示例代码
- RatingBar使用完整示例
- Python编程使用tkinter模块实现计算器软件完整代码示例
- python logging 模块完整使用示例
- Google开源html模板库ctemplate的完整使用示例
- Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api
- 使用NSIS打包一个客户端的完整示例
- struts使用list提交多行表单(提供完整示例)
- ContentObserver的使用完整详细示例
- 一小时教程 MySQL从安装到使用 详细步骤 完整图文示例练习教程 学习笔记
- 使用Filter验证用户是否登陆,完整示例
- FutureTask使用完整示例
- Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api
- jQuery使用getJSON方法获取json数据完整示例
- Cocos-2dx-Lua中使用Luaj的完整示例(转)
- struts使用list提交多行表单(提供完整示例)