C & C ++ has few template engines, and clearsilver is a famous one.
And Teng
They all have powerful functions. I need a lightweight template engine ctemplate.
The design philosophy of ctemplate is lightweight, fast, and logically isolated from the interface. Therefore, there are some differences with clearsilver and Teng. For example, ctemplate does not have a template function, and does not have conditional judgment or cyclic statements (of course, it can be implemented in a flexible way ).
1. ctemplate Introduction
The ctemplate consists of two parts: a template and a data dictionary. The template defines the form of interface presentation (V). The data dictionary is the data (m) filled with the template. You can write your own business logic to control the interface presentation (c), a typical MVC model.
The ctemplate template has four labels, and the corresponding data dictionary has different processing methods:
- Variable, {variable name}, contains the variable name in two braces. In C ++ code, values of any type can be assigned to the variable (such as characters, integer, date, etc ).
- Fragment, {# fragment name}. The fragment is represented as a sub-dictionary in the data dictionary. The dictionary can be classified. There are multi-level sub-dictionaries under the root dictionary. Fragment can be judged by processing conditions and cyclically.
- Include, {> Template Name} refers to a template that can contain other templates and corresponds to a word dictionary.
- Note ,{{! Annotation}, including annotation.
The following example demonstrates the complete four types of tags,
- <! -- Ctexample. TPL -->
- <
Html
>
- <
Head
>
<
Title
>
{Name }}
</
Title
>
- </
Head
>
- {{! This is a example of template .}}
- <
Body
>
- Hello {name }},
- You have just won $ {value }}!
- <
Table
>
- {{# In_table }}
- <
Tr
>
<
TD
>
{Item }}
</
TD
>
<
TD
>
{Taxed_value }}
</
TD
>
- </
Tr
>
- {/In_table }}
- </
Table
>
- {{
>
Included_template }}
- </
Body
>
- </
Html
>
- <! -- Ctinclude. TPL -->
-
<
Div
>
- {Include_var }}
- </
Div
>
The C ++ code is as follows:
- # Include <stdlib. h>
- # Include <string>
- # Include <iostream>
- # Include <Google/template. h>
- Int
Main (
Int
Argc,
Char
** Argv ){
- Templatedictionary dict (
"Example"
);
- Dict. setvalue (
"Name"
,
"John Smith"
);
Int
Winnings = random () %100000;
- Dict. setintvalue (
"Value"
, Winnings );
- Templatedictionary * dict1 = dict. addsectiondictionary (
"In_table"
);
- Templatedictionary * dict2 = dict. addsectiondictionary (
"In_table"
);
- Dict1-> setvalue (
"Item"
,
"Lihaibo"
);
- Dict1-> setformattedvalue (
"Taxed_value"
,
"%. 2f"
, Winnings * 0.83 );
- Dict2-> setvalue (
"Item"
,
"Qiyuehua"
);
- Dict2-> setformattedvalue (
"Taxed_value"
,
"%. 2f"
, Winnings * 0.73 );
If
(1)
- {
- Dict. showsection (
"In_table"
);
- }
- Templatedictionary * dict3 = dict. addmediadedictionary (
"Included_template"
);
- Dict3-> setfilename (
"../TPL/ctinclude. TPL"
);
- Dict3-> setvalue (
"Include_var"
,
"This is a include template ."
);
- Template * TPL = template: gettemplate (
"../TPL/ctexample. TPL"
, NWSC: do_not_strip );
- STD: String output;
- TPL-> Expand (& output, & dict );
- STD: cout <output;
- Template: Sort AchE ();
Return
0;
- }
Note:
- The template dictionary is similar to the key and value structures, and corresponds to the variable name and value.
- Fragments can have multiple records. to display a list, you can define them as fragments and retrieve multiple records and fill them in the dictionary.
- Fragments can be displayed or not displayed. If the dictionary of a piece contains data, it is displayed. If the dictionary of a piece does not contain data, it is not displayed by default. You can call showsection to display it.
2. ctemplate advanced
- Modifier (modifier), which indicates that the variable type (HTML, JS, or other) will undergo verification and encoding. For example, the HTML type will convert &
&
. Types include HTML, pre, URL query, JavaScript, CSS, and JSON. If you find it difficult to define these parameters in template variables, you can useGoogle: Template: gettemplatewithautoescaping () method,
Use the automatic replacement mode to specify HTML, JS, or CSS. You can write your own modifier to handle some special requirements.
- Strip. The template contains some blank lines and blank characters. During loading, you can specify the parameter to determine whether to clear it. For example
google::STRIP_BLANK_LINES
,google::STRIP_WHITESPACE
.
- Expandemitter, which has this interface in ctemplate. This interface is used to output data when the template is expanded. The stringemitter of STD: String version is implemented by default. After processing is completed, can be sent to the client, STD: String performance is not high. If you want an efficient web server, you can use the stream mode. For example, you can implement the expandemitter interface to stream data to the client browser.
- Dictionary copy. If the two dictionaries are similar, you can copy a dictionary, modify it, and call
dict->MakeCopy()。
- Template: Merge AchE (). Do not add this sentence when it is used. Because the template will be cached once, And the merge ache will be locked, resulting in performance degradation.