From 0bff74666e55ea75a69214dfdb12663f51beec72 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 10 Jan 2019 13:39:12 +0000 Subject: [PATCH] Change std::stringstream& to std::ostream& in render_to() (#76) * Change std::stringstream& to std::ostream& in render_to() Fixes #75 * Expose render_to * Update readme with example of render_to --- README.md | 5 +++++ include/inja/environment.hpp | 12 ++++++++++-- include/inja/renderer.hpp | 2 +- single_include/inja/inja.hpp | 14 +++++++++++--- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 64b1166..0d5eb18 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 5040c18..08072ff 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 8f868f7..e4bc338 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 db1c415..2236e5a 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