Skip to content

Commit 0bff746

Browse files
Timmmmpantor
authored andcommitted
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
1 parent 210848b commit 0bff746

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ data["name"] = "world";
1919
inja::render("Hello {{ name }}!", data); // Returns "Hello world!"
2020
```
2121
22+
`inja::render()` returns a `std::string` but you can also output to a `std::ostream&`, for example:
23+
24+
```c++
25+
inja::render_to(std::cout, "Hello {{ name }}!", data);
26+
```
2227

2328
## Integration
2429

include/inja/environment.hpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class Environment {
133133
write(temp, data, filename_out);
134134
}
135135

136-
std::stringstream& render_to(std::stringstream& os, const Template& tmpl, const json& data) {
136+
std::ostream& render_to(std::ostream& os, const Template& tmpl, const json& data) {
137137
Renderer(m_impl->included_templates, m_impl->callbacks).render_to(os, tmpl, data);
138138
return os;
139139
}
@@ -164,12 +164,20 @@ class Environment {
164164
};
165165

166166
/*!
167-
@brief render with default settings
167+
@brief render with default settings to a string
168168
*/
169169
inline std::string render(std::string_view input, const json& data) {
170170
return Environment().render(input, data);
171171
}
172172

173+
/*!
174+
@brief render with default settings to the given output stream
175+
*/
176+
inline void render_to(std::ostream& os, std::string_view input, const json& data) {
177+
Environment env;
178+
env.render_to(os, env.parse(input), data);
179+
}
180+
173181
}
174182

175183
#endif // PANTOR_INJA_ENVIRONMENT_HPP

include/inja/renderer.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class Renderer {
167167
m_tmp_args.reserve(4);
168168
}
169169

170-
void render_to(std::stringstream& os, const Template& tmpl, const json& data) {
170+
void render_to(std::ostream& os, const Template& tmpl, const json& data) {
171171
m_data = &data;
172172

173173
for (size_t i = 0; i < tmpl.bytecodes.size(); ++i) {

single_include/inja/inja.hpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ class Renderer {
14431443
m_tmp_args.reserve(4);
14441444
}
14451445

1446-
void render_to(std::stringstream& os, const Template& tmpl, const json& data) {
1446+
void render_to(std::ostream& os, const Template& tmpl, const json& data) {
14471447
m_data = &data;
14481448

14491449
for (size_t i = 0; i < tmpl.bytecodes.size(); ++i) {
@@ -1957,7 +1957,7 @@ class Environment {
19571957
write(temp, data, filename_out);
19581958
}
19591959

1960-
std::stringstream& render_to(std::stringstream& os, const Template& tmpl, const json& data) {
1960+
std::ostream& render_to(std::ostream& os, const Template& tmpl, const json& data) {
19611961
Renderer(m_impl->included_templates, m_impl->callbacks).render_to(os, tmpl, data);
19621962
return os;
19631963
}
@@ -1988,12 +1988,20 @@ class Environment {
19881988
};
19891989

19901990
/*!
1991-
@brief render with default settings
1991+
@brief render with default settings to a string
19921992
*/
19931993
inline std::string render(std::string_view input, const json& data) {
19941994
return Environment().render(input, data);
19951995
}
19961996

1997+
/*!
1998+
@brief render with default settings to the given output stream
1999+
*/
2000+
inline void render_to(std::ostream& os, std::string_view input, const json& data) {
2001+
Environment env;
2002+
env.render_to(os, env.parse(input), data);
2003+
}
2004+
19972005
}
19982006

19992007
#endif // PANTOR_INJA_ENVIRONMENT_HPP

0 commit comments

Comments
 (0)