1、AddSectionDictionary(section_name) returns a sub-dictionary associated with the given section_name (for instance, dict.AddSectionDictionary("MYSECT") for a template like {{#MYSECT}}...{{/MYSECT}}).
多次調用,每次将傳回新字典。每一次調用該函數,這個section将被展示一次。如果從沒有調用過該函數,則section區域資料将不顯示。
2、ShowSection() 用來顯示section中的文字exactly once. It is equivalent to calling AddSectionDictionary() once, and ignoring the returned sub-dictionary. All variables in the section will depend on dictionary inheritence to get their values.(繼承值)
3、SetValueAndShowSection() used to show a section if and only if a related variable is set to a non-empty value. SetValueAndShowSection(name, value, section_name) is equivalent to this: if value is empty do nothing, otherwise add a single dictionary to section_name and call section_dict->AddValue(name, value).
文檔中有例子,還沒有發現他有什麼作用。
4、AddIncludeDictionary(section_name) returns a sub-dictionary associated with the given include_name (for instance, dict.AddIncludeDictionary("MYTPL") for a template like {{>MYTPL}}).
這個函數類同AddSectionDictionary,多次調用,每次傳回新字典。調用之後,要記得調用SetFilename。
5、Dump() and DumpToString() 用來調試。
6、The TemplateCache Class
holds a collection of templates.
7、老版本使用方法:
Template* tpl = Template::GetTemplate(filename, strip_mode);
TemplateDictionary dict(name);
tpl->Expand(&dict, &outstring);
新版本使用:
std::string output;
ctemplate::ExpandTemplate("example.tpl", ctemplate::DO_NOT_STRIP, &dict, &output);
std::cout << output;
8、還可以通過指定AUTOESCAPE來指定模闆的編碼方法:
{{%AUTOESCAPE context="HTML"}}
9、還有類似lint的開發工具make_tpl_varnames_h。
10、An included template is just a section whose contents are located in a separate file. You may iterate over it just like you do sections.
11、Since sections are hidden by default, you can use represent if-else logic in your code via ShowSection. For example:
if ( my_test ) {
dict->ShowSection(kxyz_TRUE_BLOCK);
[ more code to fill the values for that section]
} else {
dict->ShowSection(kxyz_FALSE_BLOCK);
}
參考