Lexesis/src/template.cpp

42 lines
959 B
C++
Raw Normal View History

#include "Lexesis/template.h"
2016-05-25 18:30:26 +02:00
#include "mstch/mstch.hpp"
#include <fstream>
#include <string>
#include <sstream>
namespace templ {
2016-05-25 18:30:26 +02:00
TemplateContext make_string(std::string _string) {
return mstch::node(_string);
}
2016-05-25 18:59:16 +02:00
TemplateContext make_map(std::map<const std::string, TemplateContext> _map) {
return mstch::map(_map);
2016-05-25 18:30:26 +02:00
}
TemplateContext make_array(std::vector<TemplateContext> _array) {
2016-05-25 18:59:16 +02:00
return mstch::array(_array);
2016-05-25 18:30:26 +02:00
}
Template::Template(std::string filename) : m_filename(filename)
{}
Template::~Template()
{}
2016-05-25 18:30:26 +02:00
void Template::render(std::ostream& out, TemplateContext& context) {
2017-01-20 13:32:35 +01:00
mstch::config::escape = [](const std::string& str) -> std::string {
return str;
};
2016-05-25 18:30:26 +02:00
std::ifstream file;
file.open (m_filename);
std::stringstream tmplt;
tmplt << file.rdbuf();
file.close();
out << mstch::render(tmplt.str(),context);
}
}