C++模板引擎Ctemplate的介紹

來源:互聯網
上載者:User

    C&C++的模板引擎相對比較少,比較有名的是ClearSilver
和Teng
,他們功能都比較強大,我需要一個輕量級的模板引擎Ctemplate

    ctemplate的設計哲學是輕量級,快速,且邏輯和介面分離,因此和ClearSilver和Teng是有一些差異的。比如Ctemplate就沒有模板函數,沒有條件判斷和迴圈語句(當然,它可以通過變通的方式來實現)。
1.Ctemplate介紹

    ctemplate大體上分為兩個部分,一部分是模板,另一部分是資料字典。模板定義了介面展現的形式(V),資料字典就是填充模板的資料(M),你自己寫商務邏輯去控制介面展現(C),典型的MVC模型。
    ctemplate模板中有四中標記,對應的資料字典也有不同的處理方式:

  • 變數,{{變數名}},用兩個大括弧包含的就是變數名,在c++代碼中,可以對變數賦值,任何類型的值都可以(如字元,整數,日期等)。
  • 片斷,{{#片斷名}},片斷在資料字典中表現為一個子字典,字典是可以分級的,根字典下面有多級子字典。片斷可以處理條件判斷和迴圈。
  • 包含,{{>模板名}}包含指的是一個模板可以包含其他模板,對應的也是一個字字典。
  • 注釋,{{!注釋名}},包含注釋。

一份示範了完整四種標記的例子如下,

  1. <!--ctexample.tpl-->
  2. <
    html
    >
  3. <
    head
    >
  4.   
    <
    title
    >
    {{NAME}}
    </
    title
    >
  5. </
    head
    >
  6. {{!This is a example of template.}}
  7. <
    body
    >
  8. Hello {{NAME}},
  9. You have just won ${{VALUE}}!
  10. <
    table
    >
  11. {{#IN_TABLE}}
  12. <
    tr
    >
  13.     
    <
    td
    >
    {{ITEM}}
    </
    td
    >
  14.     
    <
    td
    >
    {{TAXED_VALUE}}
    </
    td
    >
  15. </
    tr
    >
  16. {{/IN_TABLE}}
  17. </
    table
    >
  18. {{
    >
    INCLUDED_TEMPLATE}}
  19. </
    body
    >
  20. </
    html
    >
  21. <!--ctinclude.tpl-->
  22.  
    <
    div
    >
  23. {{INCLUDE_VAR}}
  24. </
    div
    >

c++代碼如下

  1. #include <stdlib.h>
  2. #include <string>
  3. #include <iostream>
  4. #include <google/template.h>
  5. int
     main(
    int
     argc, 
    char
    ** argv) {
  6.     TemplateDictionary dict(
    "example"
    );
  7.     dict.SetValue(
    "NAME"

    "John Smith"
    );
  8.     
    int
     winnings = random() % 100000;
  9.     dict.SetIntValue(
    "VALUE"
    , winnings);
  10.     TemplateDictionary *dict1 = dict.AddSectionDictionary(
    "IN_TABLE"
    );
  11.     TemplateDictionary *dict2 = dict.AddSectionDictionary(
    "IN_TABLE"
    );
  12.     dict1->SetValue(
    "ITEM"

    "Lihaibo"
    );
  13.     dict1->SetFormattedValue(
    "TAXED_VALUE"

    "%.2f"
    , winnings * 0.83);
  14.     dict2->SetValue(
    "ITEM"

    "Qiyuehua"
    );
  15.     dict2->SetFormattedValue(
    "TAXED_VALUE"

    "%.2f"
    , winnings * 0.73);
  16.     
    if
     (1)
  17.     {
  18.         dict.ShowSection(
    "IN_TABLE"
    );
  19.     }
  20.     TemplateDictionary *dict3 = dict.AddIncludeDictionary(
    "INCLUDED_TEMPLATE"
    );
  21.     dict3->SetFilename(
    "../tpl/ctInclude.tpl"
    );    
  22.     dict3->SetValue(
    "INCLUDE_VAR"
    ,
    "This is a include template."
    );
  23.      Template* tpl = Template::GetTemplate(
    "../tpl/ctexample.tpl"
    ,nwsc::DO_NOT_STRIP);
  24.     std::string output;
  25.     tpl->Expand(&output, &dict);
  26.     std::cout << output;
  27.     Template::ClearCache();
  28.     
    return
     0;
  29. }

注意:

  • 模板字典類似Key和Value的結構,對應的是變數名和值。
  • 片斷是可以有多條記錄的,如果要顯示列表,可以定義為片斷,擷取多條記錄填充到字典中。
  • 片斷可以顯示,也可以不顯示。如果片斷的字典有資料,顯示。如果片斷的字典沒有資料,預設是不顯示的,可以調用ShowSection來顯示。

2.ctemplate進階

  1. Modifier(修改器),意思變數的類型(html,js或者其他),會進行校正和編碼處理,比如html類型會將&轉換成&amp
    。類型有html,pre,url query,javascript,css和json。如果覺得在模板變數中定義這些麻煩,可以在載入模板是使用google::Template::GetTemplateWithAutoEscaping()方法,
    使用自動替換模式,指定是Html,js還是css。你可以編寫你自己的modifier,來處理一些特殊的需求。
  2. Strip(清除器),模板中有一些空行和空白字元,在載入時,可以指定參數,是否需要清除。如google::STRIP_BLANK_LINES
    google::STRIP_WHITESPACE
  3. ExpandEmitter,在ctemplate中有這個介面,這個介面是在展開模板時,輸出資料用的,預設實現了std::string版本的StringEmitter,這種方式是處理完畢後,才能發送到用戶端,std::string效能並不高。如果你要一個高效率的Web伺服器,則可以用流式的模式。比如自己實現ExpandEmitter介面,實現資料流式發送到客戶瀏覽器。
  4. 字典copy,如果兩個字典很類似,可以copy一個字典,然後修改,調用dict->MakeCopy()。
  5. Template::ClearCache()這句,正式使用時不要加這句,因為模板只要用過一次,就會緩衝起來,ClearCache會加鎖,導致效能下降。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.