mustachetest.cpp 756 B

123456789101112131415161718192021222324252627282930313233
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iterator>
  5. #include "crow/mustache.h"
  6. #include "crow/json.h"
  7. using namespace std;
  8. using namespace crow;
  9. using namespace crow::mustache;
  10. string read_all(const string& filename)
  11. {
  12. ifstream is(filename);
  13. return {istreambuf_iterator<char>(is), istreambuf_iterator<char>()};
  14. }
  15. int main()
  16. {
  17. auto data = json::load(read_all("data"));
  18. auto templ = compile(read_all("template"));
  19. auto partials = json::load(read_all("partials"));
  20. set_loader([&](std::string name)->std::string
  21. {
  22. if (partials.count(name))
  23. {
  24. return partials[name].s();
  25. }
  26. return "";
  27. });
  28. context ctx(data);
  29. cout << templ.render(ctx);
  30. return 0;
  31. }