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
This commit is contained in:
Tim
2019-01-10 13:39:12 +00:00
committed by pantor
parent 210848bb98
commit 0bff74666e
4 changed files with 27 additions and 6 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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) {

View File

@@ -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