From c5ea89c87e3b3595459c07feaa30f23ac13bf05f Mon Sep 17 00:00:00 2001 From: pantor Date: Fri, 23 Feb 2018 15:45:34 +0100 Subject: [PATCH] correct write template --- README.md | 2 +- src/inja.hpp | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fa86639..19870c5 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ std::string result = env.render_template(temp, data); // "Hello Inja!" result = env.render_file("./template.txt", "./data.json"); // Or write a rendered template file -temp.write(data, "./result.txt") +env.write(temp, data, "./result.txt") env.write("./template.txt", "./data.json", "./result.txt") ``` diff --git a/src/inja.hpp b/src/inja.hpp index 3a49fdf..f4202c1 100644 --- a/src/inja.hpp +++ b/src/inja.hpp @@ -902,7 +902,7 @@ public: return renderer.render(parse(text), data); } - std::string render_template(const Template temp, json data) { + std::string render_template(const Template& temp, json data) { renderer.element_notation = element_notation; return renderer.render(temp, data); } @@ -923,11 +923,22 @@ public: file.close(); } - void write(const std::string& filename, const std::string& filename_data, const std::string& filename_out) { + void write(const Template& temp, json data, const std::string& filename_out) { + std::ofstream file(output_path + filename_out); + file << render_template(temp, data); + file.close(); + } + + void write_with_json_file(const std::string& filename, const std::string& filename_data, const std::string& filename_out) { json data = load_json(filename_data); write(filename, data, filename_out); } + void write_with_json_file(const Template& temp, const std::string& filename_data, const std::string& filename_out) { + json data = load_json(filename_data); + write(temp, data, filename_out); + } + std::string load_global_file(const std::string& filename) { return parser.load_file(input_path + filename); }