天天看點

8、ctemplate,幫助文檔,簡記(1)

1、TemplateDictionary, 用來在主函數中擴充模闆。(used to expand a template file. It is used by the main functions for expanding a template, found in template.h.)

TemplateCache,模闆對象的集合。

TemplateNamelist provides various introspection routines on collections of Template objects.

TemplateModifier and PerExpandData are used to modify the values of a TemplateDictionary at expand time. TemplateAnnotator does too, but is intended for debugging purposes. TemplateDictionaryPeer 用來測試模闆代碼。

ExpandEmitter 提供将一個擴充模闆釋出成任意的輸出。

TemplateString 類似string的類。

2、Start section and end section markers, which delimit sections which may appear zero, one, or N times in the output. Section markers look like this: {{#FOO}}...{{/FOO}}

就是我們前面翻譯的“片斷”。

3、Template-include markers look like this: {{>FOO}}。類似sections ,可以出現一次或多次。

4、ExpandTemplate():模闆系統的主要工作函數。

ctemplate::ExpandTemplate("example.tpl", ctemplate::DO_NOT_STRIP, &dict, &output);

成功,傳回真,失敗傳回false。

As always, the "filename" can also be a key to a string-based template, inserted directly into the cache via StringToTemplateCache().

Expand()是這個函數的重載版本。

ctemplate::Template* tpl = ctemplate::Template::GetTemplate("./ctexample.tpl", ctemplate::DO_NOT_STRIP);

    std::string output;

    tpl->Expand(&output, &dict);

5、設定一個模闆

ctemplate::DO_NOT_STRIP:逐字輸出模闆

ctemplate::STRIP_BLANK_LINES: 删除空行

ctemplate::STRIP_WHITESPACE:删除空行和每一行的首尾空白字元。

6、ExpandWithData()

類似ExpandTemplate

ctemplate::TemplateDictionary dict(...);

   ctemplate::PerExpandData per_expand_data;

   string output;

   ctemplate::ExpandWithData(filename, strip_mode, &output, &dict,

                             &per_expand_data);

per_expand_data為NUL,就是ExpandTemplate。

7、LoadTemplate():從磁盤(檔案)讀入模闆

loads the file into the default template cache. Future calls to ExpandTemplate() or ExpandWithData() will get the parsed template from the cache, without needing to go to disk.

read from disk, parsed, and inserted into the cache.

這個函數,會在加載過程中,把所有相關的錯誤全部detect到。This will catch all errors except for errors involving sub-templates.

8、StringToTemplateCache():從字元串讀入模闆

9、default_template_cache() and mutable_default_template_cache() 是兩個進階特性。上面講到的,ExpandTemplate(), LoadTemplate()之類,都是TemplateCache class的靜态執行個體,可以通過default_template_cache這兩個函數通路。

10、In general, an application will need to call a TemplateDictionary method for every marker in the associated template。

11、Data Dictionaries,就是一個map。

section(我們稱呼的片斷),是一系列字典資料。

整個字典結構就是一顆樹:主字典,一系列的輔字典,及包含模闆。查找maker時,由下而上查找。

12、SetValue,設定maker({{}})中變量的值,以一個string作為輸入,C++string和Cchar*都将被自動轉換成TemplateString。

dict.SetValue("NAME", "John Smith");

13、SetIntValue(),以整形值作為輸入

dict3->SetIntValue("VALUE", winnings);

14、SetFormattedValue() is a convenience routine. SetFormattedValue(key, arg1, arg2, ...) is logically equivalent to

   char buffer[A_BIG_ENOUGH_NUMBER];

   sprintf(buffer, arg1, arg2, ...);

   SetValue(key, buffer);

dict3->SetFormattedValue("TAXED_VALUE","%.2f", winnings *0.83);

15、SetGlobalValue() ,類似SetIntValue,但是針對全局字典的。Since the global dictionary(所有模闆字典共享) is shared across all template dictionaries, this is a static method on TemplateDictionary. It is thread-safe. It is also relatively slow.

    SetTemplateGlobalValue() is like SetValue(), but the values are preserved across template-includes.(僅限于包含的模闆中)。

示例代碼

模闆

8、ctemplate,幫助文檔,簡記(1)
8、ctemplate,幫助文檔,簡記(1)

View Code

邏輯代碼

8、ctemplate,幫助文檔,簡記(1)
8、ctemplate,幫助文檔,簡記(1)

結果

8、ctemplate,幫助文檔,簡記(1)
8、ctemplate,幫助文檔,簡記(1)

參考