• 6、关于ctemplate的一个例子


    1、显示循环时,可以使用{{#片断名}}

       模板字典类似KeyValue的结构,对应的是变量名和值。

       片断是可以有多条记录的,如果要显示列表,可以定义为片断,获取多条记录填充到字典中。

       片断可以显示,也可以不显示。如果片断的字典有数据,显示。如果片断的字典没有数据,默认是不显示的,可以调用ShowSection来显示。

    2、一个结合了片断名,包含模板的示例

    模板1

    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>

    模板2

    example.tpl
    Hello{{NAME}},
    You have just won ${{VALUE}}!
    {{#IN_CA}}
    Well, ${{TAXED_VALUE}}, after taxes.
    {{/IN_CA}}

    逻辑代码

    View Code
    #include <stdlib.h>
    #include
    <string>
    #include
    <iostream>
    #include
    <ctemplate/template.h>

    int main( int argc, char ** argv)
    {
    ctemplate::TemplateDictionary dict(
    "ctexample");
    dict.SetValue(
    "NAME", "John Smith");
    int winnings = random() % 100000;
    dict.SetIntValue(
    "VALUE", winnings);
    ctemplate::TemplateDictionary
    *dict1 = dict.AddSectionDictionary( "IN_TABLE" );
    ctemplate::TemplateDictionary
    *dict2 = dict.AddSectionDictionary( "IN_TABLE" );
    dict1
    ->SetValue("ITEM", "Lihaibo");
    dict1
    ->SetFormattedValue("TAXED_VALUE", "%.2f", winnings * 0.83);
    dict2
    ->SetValue("ITEM", "Qiyuehua");
    dict.SetValue(
    "INCLUDE_VAR", "Qiyuehua");
    dict2
    ->SetFormattedValue("TAXED_VALUE", "%.2f", winnings * 0.73);
    if (1)
    {
    dict.ShowSection(
    "IN_TABLE" );
    }
    ctemplate::TemplateDictionary
    *dict3 = dict.AddIncludeDictionary("INCLUDED_TEMPLATE" );
    dict3
    ->SetFilename("./example.tpl" );
    dict3
    ->SetValue("INCLUDE_VAR" , "This is a include template.");
    dict3
    ->SetValue("NAME", "MEEE");
    dict3
    ->SetIntValue("VALUE", winnings);
    dict3
    ->SetFormattedValue("TAXED_VALUE","%.2f", winnings *0.83);
    if(1)
    {
    dict.ShowSection(
    "IN_CA");
    }
    ctemplate::Template
    * tpl = ctemplate::Template::GetTemplate("./ctexample.tpl", ctemplate::DO_NOT_STRIP);
    std::
    string output;
    tpl
    ->Expand(&output, &dict);
    std::cout
    << output;
    ctemplate::Template::ClearCache();
    return 0;
    }

    结果

    View Code
    <!--ctexample.tpl-->
    <html>
    <head>
    <title> John Smith </title>
    </head>

    <body>
    Hello John Smith,
    You have just won $
    89383!
    <table>

    <tr>
    <td> Lihaibo </td>
    <td> 74187.89 </td>
    </tr>

    <tr>
    <td> Qiyuehua </td>
    <td> 65249.59 </td>
    </tr>

    </table>
    HelloMEEE,
    You have just won $
    89383!


    </body>
    </html>
    <!--ctinclude.tpl-->
    <div>
    Qiyuehua
    </div>

    参考

    1http://www.blogjava.net/xiaomage234/archive/2007/12/24/170174.html

    2http://baike.baidu.com/view/5835966.htm

    3http://zsulwj.blog.163.com/blog/static/35326925200811934454946/

    4ctemplate安装包下载地址:

    http://code.google.com/p/google-ctemplate/downloads/list

    5】示例程序

    http://code.google.com/p/google-ctemplate/

    6http://hi.baidu.com/duanmuchun/blog/item/eab82273b1a8861b8701b0bd.html

  • 相关阅读:
    59、web框架以及Django框架
    58、bootstrap
    56、jQuery事件
    55、原生js事件与jQuery
    36-并发编程
    4.20---远程执行命令的CS架构软件
    35-socket 基于套接字的TCP与UDP
    34-网络编程
    33-异常处理
    4.15---元类练习
  • 原文地址:https://www.cnblogs.com/mydomain/p/2169084.html
Copyright © 2020-2023  润新知