Introduction to C ++ template engine ctemplate

Source: Internet
Author: User

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,

  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
    >

The C ++ code is as follows:

  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. addmediadedictionary (
    "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: Sort AchE ();

  28. Return
    0;
  29. }

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

  1. Modifier (modifier), which indicates that the variable type (HTML, JS, or other) will undergo verification and encoding. For example, the HTML type will convert &&amp
    . 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.
  2. Strip. The template contains some blank lines and blank characters. During loading, you can specify the parameter to determine whether to clear it. For examplegoogle::STRIP_BLANK_LINES
    ,google::STRIP_WHITESPACE
    .
  3. 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.
  4. Dictionary copy. If the two dictionaries are similar, you can copy a dictionary, modify it, and calldict->MakeCopy()。
  5. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.