mirror of
https://github.com/pantor/inja.git
synced 2026-04-04 23:28:52 +00:00
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:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user