• 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

  • 相关阅读:
    戴尔服务器状态信息和简单处理
    zabbix3.2通过snmp v2采集Dell服务器iDRAC口信息监控硬件
    戴尔服务器使用omreport(OMSA)查看监控硬件信息
    SVN主从高可用
    Linux下ping命令参数详细解析
    记录脚本运行时间
    Centos6优化系统服务脚本
    git分支管理
    git基本操作
    开源CMDB详细安装使用
  • 原文地址:https://www.cnblogs.com/mydomain/p/2169084.html
Copyright © 2020-2023  润新知