ctemplate

ctemplate (Google-ctemplate)的設計哲學是輕量級,快速,且邏輯和界面分離。

基本介紹

  • 外文名:ctemplate
  • 全稱:Google-ctemplate)
  • 設計哲學:輕量級,快速,且邏輯和界面分離
  • 特點:沒有模板函式
簡介,處理方式,

簡介

比如Ctemplate就沒有模板函式,沒有條件判斷和循環語句(當然,它可以通過變通的方式來實現)。
ctemplate大體上分為兩個部分,一部分是模板,另一部分是數據字典。模板定義了界面展現的形式(V),數據字典就是填充模板的數據(M),你自己寫業務邏輯去控制界面展現(C),典型的MVC模型。

處理方式

* 變數,{{變數名}},用兩個大括弧包含的就是變數名,在c++代碼中,可以對變數賦值,任何類型的值都可以(如字元,整數,日期等)。
* 片斷,{{#片斷名}},片斷在數據字典中表現為一個子字典,字典是可以分級的,根字典下面有多級子字典。片斷可以處理條件判斷和循環。
* 包含,{{>模板名}}包含指的是一個模板可以包含其他模板,對應的也是一個字字典。
* 注釋,{{!注釋名}},包含注釋。
Here is a simple template file:
Hello{{NAME}},
You have just won ${{VALUE}}!
{{#IN_CA}}Well, ${{TAXED_VALUE}}, after taxes.{{/IN_CA}}
Here is a C++ program to fill in the template, which we assume is stored in the file example.tpl':
#include<stdlib.h>
#include<string>
#include<iostream>
#include<ctemplate/template.h>
int main(int argc,char** argv){
ctemplate::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");
}
std::string output;
ctemplate::ExpandTemplate("example.tpl", ctemplate::DO_NOT_STRIP,&dict,&output);
std::cout << output;
return0;
}

相關詞條

熱門詞條

聯絡我們