Ctemplate的簡介

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是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>

 

發佈了55 篇原創文章 · 獲贊 18 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章