• 使用google 模版引擎生成html页面


    在一个项目中要把测试结果以HTML的形式上传到CI平台上,使用了google的 C++模版引擎。其他的模版引擎可以在wikipedia上看到:

    http://en.wikipedia.org/wiki/Template_engine_(web)

    公司广泛使用的velocity也在其中啊,据说python的Mako非常好用。 

    #include <stdlib.h> 
    #include <string
    #include <iostream>   
    #include "template.h"
    #include "template_dictionary.h"
    #include "template_cache.h"
    using namespace std;
    using namespace ctemplate;

        int main(int argc, char** argv) { 
            if(argc != 2)
            {   
                printf("usage:%s count\n", argv[0]);
                return 1;
            }   
            int count = atoi(argv[1]);
            TemplateDictionary dict("json");

            for(int i = 0; i < count;i++)
            {   
                TemplateDictionary *dict1 = dict.AddSectionDictionary( "data" );
                dict1->SetValue("CASENO","200");    
                dict1->SetValue("USRID","1");    
                dict1->SetValue("SEX","m");    
                dict1->SetValue("AGE","20");    
                dict1->SetValue("NICKNAME","测试");    
                dict1->SetValue("PROVINCE","广东");    
            }   
            dict.SetIntValue("TOTAL"10);
            dict.SetIntValue("SUCCESS"4); 
            dict.SetIntValue("FAILED"6); 

            if(1)
            {   
                dict.ShowSection( "USERINFO_LIST" );  
            }   

            std::string output; 
            ctemplate::ExpandTemplate("./iprocess_template.tpl", ctemplate::DO_NOT_STRIP, &dict, &output); 
            std::cout << output; 
            return 0;  

        }    

    使用的template如下: 

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html lang="en">

    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Iprocess Events Trigger Automatic-Testing Report</title>
      
      <link rel="stylesheet" type="text/css" href="gcov.css">
    </head>

    <body>

      <table width="100%" border=0 cellspacing=0 cellpadding=0>
        <tr><td class="title">Iprocess Events Trigger Automatic-Testing Report</td></tr>
        <tr><td class="ruler"><img src="glass.png" width=3 height=3 alt=""></td></tr>
        <tr><td class="ruler"><img src="glass.png" width=3 height=3 alt=""></td></tr>
      </table>

        <tr>
          <td width="100%">
            <table cellpadding=1 border=0 width="100%">
              <tr>
                <td width="10%" class="headerItem">CASE TOTAL NUM:</td>
                <td width="35%" class="headerValue">{{TOTAL}}</td>

              </tr>
              <tr>
                <td class="headerItem">SUCCESS NUM::</td>
                <td class="headerValue">{{SUCCESS}}</td>
                <td></td>

              </tr>
              <tr>
                <td class="headerItem">FAILED NUM:</td>
                <td class="headerValue">{{FAILED}}</td>
                <td></td>

            </table>
          </td>
        </tr>

      <center>
      <table width="90%" cellpadding=1 cellspacing=1 border=0>

        <tr>
          <td width="10%"><br></td>
          <td width="30%"></td>
          <td width="40%"></td>
          <td width="20%"></td>
        </tr>

        <tr>
          <td class="tableHead">CASE NO.<span class="tableHeadSort"></span></td>
          <td class="tableHead" colspan=1>SEGMENT<span class="tableHeadSort"></span></td>
          <td class="tableHead" colspan=1>ROW_KEY<span class="tableHeadSort"></span></td>
          <td class="tableHead" colspan=1>RESULT<span class="tableHeadSort"></span></td>
        </tr>

        {{#data}}
        <tr>
          <td class="coverFile">{{CASENO}}</td>
          <td class="coverBar" align="center">{{SEGMENT}}</td>
          <td class="coverFile">{{ROW_KEY}}</td>
          <td class="coverFile">{{RESULT}}</td>
        </tr>
        {{/data}}
      </table>
      </center>
      <br>

    </body>

    </html> 

  • 相关阅读:
    文件重名问题
    文件上传
    回顾IO流
    Freemarker
    中文乱码问题
    Filter(过滤器)
    Ajax
    jQuery
    普华操作系统,开机无法进入桌面程序; 解决多次source /etc/profile的烦恼
    C++ 文件类型判别错误,将目录文件识别为普通文件
  • 原文地址:https://www.cnblogs.com/welkinwalker/p/2266273.html
Copyright © 2020-2023  润新知