在線oj之頁面渲染模塊

頁面渲染模塊的作用是將我們從後臺獲取到的數據渲染到html頁面上。爲此,我們採用了geogle的開源html模板庫ctemplate,具體介紹可以百度。
我使用的是碼雲上大神編譯好的(只支持centOS7),其他環境需要自己編譯。
centos7版本,可以直接用
其他環境就需要自己在github上下載源碼自己編譯

#pragma once

#include "ctemplate/template.h"
#include <string>
#include <vector>
#include "oj_model.hpp"


class OjView
{

    public:
        //渲染html頁面,並且返回頁面·
        static void ExpandAllQuestionsHtml(std::string* html, std::vector<Question>& ques) {
            //獲取數據字典-》將拿到的試題數據按照一定順序保存到內存中
            ctemplate::TemplateDictionary dict("all_questions");

            for (const auto& que: ques) {
                ctemplate::TemplateDictionary* section_dict = dict.AddSectionDictionary("question");
               section_dict->SetValue("id", que._id);
               section_dict->SetValue("id", que._id);
               section_dict->SetValue("name", que._name);
               section_dict->SetValue("star", que._star);
            }
            //2.獲取模板類的類指針,加載預定義的html頁面到內存中
            ctemplate::Template* tl = ctemplate::Template::GetTemplate("./template/all_questions.html", ctemplate::DO_NOT_STRIP);
            //3.渲染,拿着模板類的指針,將數據字典當中的數據更新到html頁面中

            tl->Expand(html, &dict);
        }


        //id name star desc header
        //ti'mu
        static void ExpandOneQuestion(const Question ques, std::string& desc, std::string& header, std::string* html)
        {
            ctemplate::TemplateDictionary dict("question");
            dict.SetValue("id", ques._id);
            dict.SetValue("name", ques._name);
            dict.SetValue("star", ques._star);
            dict.SetValue("desc", desc);
            dict.SetValue("header", header);
            ctemplate::Template* tpl = ctemplate::Template::GetTemplate("./template/question.html", ctemplate::DO_NOT_STRIP);
            tpl->Expand(html, &dict);
        }


        static void ExpandReason(const std::string& errorno, const std::string& reason, const std::string& stdout_reason, std::string* html)
        {

            ctemplate::TemplateDictionary dict("reason");
            dict.SetValue("errono", errorno);
            dict.SetValue("reason", reason);
            dict.SetValue("stdout", stdout_reason);
            ctemplate::Template* tpl = ctemplate::Template::GetTemplate("./template/reason.html", ctemplate::DO_NOT_STRIP);
            tpl->Expand(html, &dict);
        }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章