C++的html模板库——google-ctemplete

C++也能用来写网页,Google的大佬们开发的google-ctemplete,就是一个很好的用于写网页的库。


一.下载安装包

下载地址:点击打开链接

点开之后直接点击download下载就完事了

二.安装

首先解压安装包,完了以后

  1. ./configure
  2. make
  3. sudo make install

这时因为我们在./configure时没有加上选项给上安装的路径,就自动安装到/usr/local/lib库中了。


三.简单案例

example.tpl程序如下(这本质上就是一个html网页,只是是一个模板网页)

{{NAME}}你好 , 
   恭喜你中奖了,奖金总额是:$ {{VALUE}}! 
{{#IN_CA}}您应缴纳的税金总额为: ${{TAXED_VALUE}}。 {{/IN_CA}}

test.cc文件如下

#include <stdlib.h> 
#include <string> 
#include <iostream>   
#include <google/template.h>   
int main ( int argc , char ** argv  ) { 
  google::TemplateDictionary dict( "example"); 
  dict.SetValue ( "NAME" , "John Smith"  ); 
  int winnings = rand () % 100000 ; 
  dict.SetIntValue ( "VALUE" , winnings  ); 
  dict.SetFormattedValue ( "TAXED_VALUE" , "%.2f" , winnings * 0.83  ); 
  // For now, assume everyone lives in CA. 
  //   // (Try running the program with a 0 here instead!) 
  if ( 1  ) { 
      dict . ShowSection ( "IN_CA"  ); 
  } 
  google :: Template * tpl = google :: Template :: GetTemplate ( "example.tpl" , 
                                                                 google :: DO_NOT_STRIP ); 
  std :: string output ; 
  tpl -> Expand (& output , & dict ); 
  std :: cout << output ; 
  return 0 ; 
}
显示出来可能有点乱,直接复制粘贴出来就完事了。

在我们使用g++编译的时候,需要加上响应的选项

g++ test.cc -o test -lctemplate -lpthread -std=c++11

编译出来可能会有一些警告,暂时不管


这个时候执行./test可能还会有如下的问题


把/usr/local/lib下的libctemplate.so.0拷贝一份到/usr/lib下就可以了。

再次执行,成功替换了。



刚接触这个库,感觉功能很强大,今天先溜了,日后补上用法!!!





猜你喜欢

转载自blog.csdn.net/lvyibin890/article/details/80831686
今日推荐