使用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> 

 

 

原文链接: https://www.cnblogs.com/welkinwalker/archive/2011/11/28/2266273.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/37465

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午2:06
下一篇 2023年2月8日 下午2:07

相关推荐