简介:google ctemplate:简单易用的文字模板
2014-07-15 18:30
357 查看
http://blog.csdn.net/aladdina/article/details/4531736
CTemplate 是一个简单实用、功能强大的文字模板(template language),适用于使用C++语言开发的应用程序。 其解决的主要问题是将文字表达和逻辑分离开来:文字模板解决如何用合适的文字和形式来表达的问题,而逻辑问题则由文字模板的调用者在源代码中完成。
下面有一个简单的例子让我们初步了解其概念,介绍了如何在你的程序中应用CTemplate:
首先创建一个模板文件,命名为example.tpl,以文本方式输入以下内容:
{{ NAME }}你好 ,
恭喜你中奖了,奖金总额是:$ {{ VALUE }}!
{{ #IN_CA}}您应缴纳的税金总额为: ${{TAXED_VALUE}}。 {{/IN_CA}}
在C++程序中我们可以这样调用:
#include <stdlib.h>
#include <string>
#include <iostream>
#include <google/template.h>
int main ( int argc , char ** argv ) {
google :: TemplateDictionary dict ( "example" );
dict . SetValue ( "NAME" , "John
Smith" );
int winnings = rand () % 100000 ;
dict . SetIntValue ( "VALUE" , winnings );
dict . SetFormattedValue ( "TAXED_VALUE" , "%.2f" , winnings * 0.83 );
// For now, assume everyone lives in CA.
// (Try running the program with a 0 here instead!)
if ( 1 ) {
dict . ShowSection ( "IN_CA" );
}
google :: Template * tpl = google :: Template :: GetTemplate ( "example.tpl" ,
google :: DO_NOT_STRIP );
std :: string output ;
tpl -> Expand (& output , & dict );
std :: cout << output ;
return 0 ;
}
如果你感兴趣的话可以参考完整的帮助文档:How To Use the Google Template System
CTemplate 是一个简单实用、功能强大的文字模板(template language),适用于使用C++语言开发的应用程序。 其解决的主要问题是将文字表达和逻辑分离开来:文字模板解决如何用合适的文字和形式来表达的问题,而逻辑问题则由文字模板的调用者在源代码中完成。
下面有一个简单的例子让我们初步了解其概念,介绍了如何在你的程序中应用CTemplate:
首先创建一个模板文件,命名为example.tpl,以文本方式输入以下内容:
{{ NAME }}你好 ,
恭喜你中奖了,奖金总额是:$ {{ VALUE }}!
{{ #IN_CA}}您应缴纳的税金总额为: ${{TAXED_VALUE}}。 {{/IN_CA}}
在C++程序中我们可以这样调用:
#include <stdlib.h>
#include <string>
#include <iostream>
#include <google/template.h>
int main ( int argc , char ** argv ) {
google :: TemplateDictionary dict ( "example" );
dict . SetValue ( "NAME" , "John
Smith" );
int winnings = rand () % 100000 ;
dict . SetIntValue ( "VALUE" , winnings );
dict . SetFormattedValue ( "TAXED_VALUE" , "%.2f" , winnings * 0.83 );
// For now, assume everyone lives in CA.
// (Try running the program with a 0 here instead!)
if ( 1 ) {
dict . ShowSection ( "IN_CA" );
}
google :: Template * tpl = google :: Template :: GetTemplate ( "example.tpl" ,
google :: DO_NOT_STRIP );
std :: string output ;
tpl -> Expand (& output , & dict );
std :: cout << output ;
return 0 ;
}
如果你感兴趣的话可以参考完整的帮助文档:How To Use the Google Template System
相关文章推荐
- 简介:google ctemplate:简单易用的文字模板(转载)
- 简介:google ctemplate:简单易用的文字模板
- 简介:google ctemplate:简单易用的文字模板
- 出位的template.js 基于jquery的模板渲染插件,简单、好用
- (原创)微信公众号开发的简单模板及配置流程简介
- Velocity学习笔记3——Velocity 模板语言(VTL: Velocity Template Language)简介
- 【opencv学习之三十】简单模板匹配matchTemplate
- x-template:一个简单高效的字符串模板引擎
- opencv输出文字 opencv简单的图像模板匹配代码
- 中级JavaScript例子, 如何实现一个简单实用的模板套用机制, GXTemplate , 第4章(估计要写9章)
- 玄宇说:简单的VTemplate模板引擎的使用
- C++基础:C++模板(Template)简介
- artTemplate模板引擎简单介绍使用
- [C/C++]_[简单例子看模板template的特性]
- x-template:一个简单高效的字符串模板引擎
- 简单的mvc twig模板引擎和框架的结束语的简介
- [C/C++]_[简单例子看模板template的特性]
- 中级JavaScript例子, 如何实现一个简单实用的模板套用机制, GXTemplate , 第一章(估计要写9章)
- [开源推荐]简单强大的模板语言JSON Template
- 简单易用的baidutemplate模板的使用