Modify create view command of drogon_ctl

This commit is contained in:
antao 2018-10-05 19:17:12 +08:00
parent 0382d2ac83
commit 0c6f39c34d
4 changed files with 55 additions and 10 deletions

View File

@ -23,6 +23,8 @@ static const std::string cxx_end="%>";
static const std::string cxx_lang="<%c++";
static const std::string cxx_view_data="@@";
static const std::string cxx_output="$$";
static const std::string cxx_val_start="{{";
static const std::string cxx_val_end="}}";
using namespace drogon_ctl;
@ -55,7 +57,17 @@ static void parseCxxLine(std::ofstream &oSrcFile,const std::string& line,const s
}
}
static void outputVal(std::ofstream &oSrcFile,const std::string& streamName,const std::string& viewDataName,const std::string &keyName)
{
oSrcFile<<"{\n";
oSrcFile<<" auto & val="<<viewDataName<<"[\""<<keyName<<"\"];\n";
oSrcFile<<" if(val.type()==typeid(const char *)){\n";
oSrcFile<<" "<<streamName<<"<<any_cast<const char *>(val);\n";
oSrcFile<<" }else if(val.type()==typeid(std::string)||val.type()==typeid(const std::string)){\n";
oSrcFile<<" "<<streamName<<"<<any_cast<const std::string>(val);\n";
oSrcFile<<" }\n";
oSrcFile<<"}\n";
}
static void parseLine(std::ofstream &oSrcFile,std::string& line,const std::string& streamName,const std::string& viewDataName,int &cxx_flag,int returnFlag=1)
{
std::string::size_type pos(0);
@ -74,12 +86,41 @@ static void parseLine(std::ofstream &oSrcFile,std::string& line,const std::strin
}
else
{
if(line.length()>0)
oSrcFile<<"\t"<<streamName<<" << \""<<replace_all(line,"\"","\\\"");
if(returnFlag)
oSrcFile<<"\\n\";\n";
if((pos=line.find(cxx_val_start))!=std::string::npos)
{
std::string oldLine=line.substr(0,pos);
parseLine(oSrcFile,oldLine,streamName,viewDataName,cxx_flag,0);
std::string newLine=line.substr(pos+cxx_val_start.length());
if((pos=newLine.find(cxx_val_end))!=std::string::npos)
{
std::string keyName=newLine.substr(0,pos);
auto iter=keyName.begin();
while(iter!=keyName.end()&&*iter==' ')
iter++;
auto iterEnd=iter;
while(iterEnd!=keyName.end()&&*iterEnd!=' ')
iterEnd++;
keyName=std::string(iter,iterEnd);
outputVal(oSrcFile,streamName,viewDataName,keyName);
std::string tailLine=newLine.substr(pos+cxx_val_end.length());
parseLine(oSrcFile,tailLine,streamName,viewDataName,cxx_flag,returnFlag);
}
else
{
std::cerr<<"format err!"<<std::endl;
exit(1);
}
}
else
oSrcFile<<"\";\n";
{
if(line.length()>0)
oSrcFile<<"\t"<<streamName<<" << \""<<replace_all(line,"\"","\\\"");
if(returnFlag)
oSrcFile<<"\\n\";\n";
else
oSrcFile<<"\";\n";
}
}
}
else

View File

@ -3,7 +3,7 @@ void ListParaCtl::asyncHandleHttpRequest(const HttpRequestPtr& req,const std::fu
{
//write your application logic here
HttpViewData data;
data.insert("title",std::string("list parameters"));
data.insert("title","list parameters");
data.insert("parameters",req->getParameters());
auto res=drogon::HttpResponse::newHttpViewResponse("ListParaView.csp",data);
callback(res);

View File

@ -1,11 +1,11 @@
<!DOCTYPE html>
<html>
<%c++ std::string title=@@.get<std::string>("title");
<%c++
auto para=@@.get<std::map<std::string,std::string>>("parameters");
%>
<head>
<meta charset="UTF-8">
<title><%c++ $$<<title;%> </title>
<title>{{ title }}</title>
</head>
<body>
<%c++ if(para.size()>0){%>

View File

@ -52,7 +52,11 @@ public:
{
viewData_[key]=obj;
}
any& operator [] (const std::string &key) const
{
return viewData_[key];
}
protected:
ViewDataMap viewData_;
mutable ViewDataMap viewData_;
};
}