Skip to content

Commit

Permalink
Change std::stringstream& to std::ostream& in render_to() (#76)
Browse files Browse the repository at this point in the history
* Change std::stringstream& to std::ostream& in render_to()

Fixes #75

* Expose render_to

* Update readme with example of render_to
  • Loading branch information
Timmmm authored and pantor committed Jan 10, 2019
1 parent 210848b commit 0bff746
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 10 additions & 2 deletions include/inja/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 11 additions & 3 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0bff746

Please sign in to comment.