diff --git a/README.md b/README.md index 64b11662..0d5eb184 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,11 @@ data["name"] = "world"; inja::render("Hello {{ name }}!", data); // Returns "Hello world!" ``` +`inja::render()` returns a `std::string` but you can also output to a `std::ostream&`, for example: + +```c++ +inja::render_to(std::cout, "Hello {{ name }}!", data); +``` ## Integration diff --git a/include/inja/environment.hpp b/include/inja/environment.hpp index 5040c183..08072ff4 100644 --- a/include/inja/environment.hpp +++ b/include/inja/environment.hpp @@ -133,7 +133,7 @@ class Environment { write(temp, data, filename_out); } - std::stringstream& render_to(std::stringstream& os, const Template& tmpl, const json& data) { + std::ostream& render_to(std::ostream& os, const Template& tmpl, const json& data) { Renderer(m_impl->included_templates, m_impl->callbacks).render_to(os, tmpl, data); return os; } @@ -164,12 +164,20 @@ class Environment { }; /*! -@brief render with default settings +@brief render with default settings to a string */ inline std::string render(std::string_view input, const json& data) { return Environment().render(input, data); } +/*! +@brief render with default settings to the given output stream +*/ +inline void render_to(std::ostream& os, std::string_view input, const json& data) { + Environment env; + env.render_to(os, env.parse(input), data); +} + } #endif // PANTOR_INJA_ENVIRONMENT_HPP diff --git a/include/inja/renderer.hpp b/include/inja/renderer.hpp index 8f868f74..e4bc338e 100644 --- a/include/inja/renderer.hpp +++ b/include/inja/renderer.hpp @@ -167,7 +167,7 @@ class Renderer { m_tmp_args.reserve(4); } - void render_to(std::stringstream& os, const Template& tmpl, const json& data) { + void render_to(std::ostream& os, const Template& tmpl, const json& data) { m_data = &data; for (size_t i = 0; i < tmpl.bytecodes.size(); ++i) { diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp index db1c4151..2236e5ac 100644 --- a/single_include/inja/inja.hpp +++ b/single_include/inja/inja.hpp @@ -1443,7 +1443,7 @@ class Renderer { m_tmp_args.reserve(4); } - void render_to(std::stringstream& os, const Template& tmpl, const json& data) { + void render_to(std::ostream& os, const Template& tmpl, const json& data) { m_data = &data; for (size_t i = 0; i < tmpl.bytecodes.size(); ++i) { @@ -1957,7 +1957,7 @@ class Environment { write(temp, data, filename_out); } - std::stringstream& render_to(std::stringstream& os, const Template& tmpl, const json& data) { + std::ostream& render_to(std::ostream& os, const Template& tmpl, const json& data) { Renderer(m_impl->included_templates, m_impl->callbacks).render_to(os, tmpl, data); return os; } @@ -1988,12 +1988,20 @@ class Environment { }; /*! -@brief render with default settings +@brief render with default settings to a string */ inline std::string render(std::string_view input, const json& data) { return Environment().render(input, data); } +/*! +@brief render with default settings to the given output stream +*/ +inline void render_to(std::ostream& os, std::string_view input, const json& data) { + Environment env; + env.render_to(os, env.parse(input), data); +} + } #endif // PANTOR_INJA_ENVIRONMENT_HPP