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=; i<; ++i)
{
ctemplate::TemplateDictionary* table1_dict;
table1_dict = dict.AddSectionDictionary("TABLE1");
table1_dict->SetValue("field1", "");
table1_dict->SetValue("field2", ""); // 这里有点类似于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 ;
}

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>

google ctemplate——c++模板引擎的更多相关文章

  1. java开源模板引擎

      Velocity  Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义 ...

  2. 浅谈 Web 中前后端模板引擎的使用

    前言 这篇文章本来不打算写的,实话说楼主对前端模板的认识还处在非常初级的阶段,但是为了整个 源码解读系列 的完整性,在深入 Underscore _.template 方法源码后,觉得还是有必要记下此 ...

  3. 基于.NET的免费开源的模板引擎---VTemplate(转)

    1.VTemplate模板引擎的简介 VTemplate模板引擎也简称为VT,是基于.NET的模板引擎,它允许任何人使用简单的类似HTML语法的模板语言来引用.NET里定义的对象.当VTemplate ...

  4. express-9 Handlebars模板引擎(2)

    视图和布局 视图通常表现为网站上的各个页面(它也可以表现为页面中AJAX局部加载的内容,或一封电子邮件,或页面上的任何东西).默认情况下,Express会在views子目录中查找视图.布局是一种特殊的 ...

  5. JST(JavaScript Trimpath)前端模板引擎简介

    JST(JavaScript Trimpath)前端模板引擎简介及应用 今天在做某系统日志列表的时候用到了这个玩意儿.刚开始只是根据别人的例子照葫芦画瓢完成了日志列表及对应详情,晚上有空了才仔细去网上 ...

  6. 模板引擎 Velocity

    模板引擎 Velocity 一.Velocity简介: Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template langu ...

  7. 【转】在Express项目中使用Handlebars模板引擎

    原文:http://fraserxu.me/2013/09/12/Using-Handlebarsjs-with-Expressjs/ 最近在用Expressjs做一个项目,前后端都用它来完成.自己之 ...

  8. Jade —— 源于 Node.js 的 HTML 模板引擎

    2013-12-11 发布 Jade —— 源于 Node.js 的 HTML 模板引擎 开源项目介绍 web 模板引擎 node.js jade 207.8k 次阅读  ·  读完需要 69 分钟 ...

  9. node(基础)_node.js中的http服务以及模板引擎的渲染

    一.前言 本节的内容主要涉及: 1.node.js中http服务 2.node.js中fs服务 3.node.js中模板引擎的渲染 4.利用上面几点模拟apache服务器 二.知识 1.node.js ...

随机推荐

  1. TCP端口转发(centos7)

    =============================================== 2019/2/14_第1次修改                       ccb_warlock == ...

  2. java selenium操作safari

    SafariDriver是一个Safari浏览器的扩展.和Firefox类似,在selenium-safari-driver-xxx.jar包中org.openqa.selenium.safari路径 ...

  3. Laravel Blade 模板 @section/endsection 与 @section/show, @yield 的区别

    base layout 中需要使用 @section("section_name") 区块链是什么? @show 继承的 blade 中需要使用 @section("se ...

  4. python+selenium二:定位方式

    # 八种单数定位方式:elementfrom selenium import webdriverimport time driver = webdriver.Firefox()time.sleep(2 ...

  5. python 全栈开发,Day120(路由系统, 实例化Flask的参数, 蓝图(BluePrint), before_request after_request)

    昨日内容回顾 1.Flask: from flask import Flask app = Flask(__name__) # 从源码中可以看出,Flask集成的run方法是由werkzeug中的ru ...

  6. 使用spring-boot-starter-data-jpa 怎么配置使运行时输出SQL语句

    在 application.properties 中加入以下配置 spring.jpa.show-sql=true

  7. java中final,finally,finalize三个关键字的区别

    final 可以作为修饰符修饰变量.方法和类. 被final修饰的变量必须在初始化时给定初值,以后在使用时只能被引用而不能被修改. 被final修饰的方法不能够在子类中被重写(override): 被 ...

  8. Oracle回收站的清理方法

    http://blog.itpub.net/18841027/viewspace-1057765/

  9. sed & awk之sed

    sed处理文本的方法 sed在处理文本时,会先读取第一个输入行,将编辑命令应用于输入行,然后读取下一个输入行,并应用编辑命令.sed总是处理最新版本的行,因此sed中有多个编辑命令时,编辑命令的顺序对 ...

  10. BZOJ3674 可持久化并查集加强版 可持久化 并查集

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3674 题意概括 n个集合 m个操作操作:1 a b 合并a,b所在集合2 k 回到第k次操作之后的 ...