Skip to content

Commit

Permalink
add option to .dump() strings instead of .get<std::string>()
Browse files Browse the repository at this point in the history
  • Loading branch information
jbohanon committed Jul 19, 2023
1 parent c2ad85d commit 0d15c31
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/inja/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ struct ParserConfig {
*/
struct RenderConfig {
bool throw_at_missing_includes {true};
bool escape_strings {};
};

} // namespace inja
Expand Down
5 changes: 5 additions & 0 deletions include/inja/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class Environment {
lexer_config.lstrip_blocks = lstrip_blocks;
}

/// Sets the config for rendering strings raw or escaped
void set_escape_strings(bool escape_strings) {
render_config.escape_strings = escape_strings;
}

/// Sets the element notation syntax
void set_search_included_templates_in_files(bool search_in_files) {
parser_config.search_included_templates_in_files = search_in_files;
Expand Down
2 changes: 1 addition & 1 deletion include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Renderer : public NodeVisitor {
}

void print_data(const std::shared_ptr<json> value) {
if (value->is_string()) {
if (value->is_string() && !config.escape_strings) {
*output_stream << value->get_ref<const json::string_t&>();
} else if (value->is_number_integer()) {
*output_stream << value->get<const json::number_integer_t>();
Expand Down
8 changes: 7 additions & 1 deletion single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ struct ParserConfig {
*/
struct RenderConfig {
bool throw_at_missing_includes {true};
bool escape_strings {};
};

} // namespace inja
Expand Down Expand Up @@ -2123,7 +2124,7 @@ class Renderer : public NodeVisitor {
}

void print_data(const std::shared_ptr<json> value) {
if (value->is_string()) {
if (value->is_string() && !config.escape_strings) {
*output_stream << value->get_ref<const json::string_t&>();
} else if (value->is_number_integer()) {
*output_stream << value->get<const json::number_integer_t>();
Expand Down Expand Up @@ -2772,6 +2773,11 @@ class Environment {
lexer_config.lstrip_blocks = lstrip_blocks;
}

/// Sets the config for rendering strings raw or escaped
void set_escape_strings(bool escape_strings) {
render_config.escape_strings = escape_strings;
}

/// Sets the element notation syntax
void set_search_included_templates_in_files(bool search_in_files) {
parser_config.search_included_templates_in_files = search_in_files;
Expand Down
6 changes: 6 additions & 0 deletions test/test-renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ TEST_CASE("types") {
data["relatives"]["brother"] = "Chris";
data["relatives"]["sister"] = "Jenny";
data["vars"] = {2, 3, 4, 0, -1, -2, -3};
data["quoted"] = "\"quoted value\"";

SUBCASE("basic") {
CHECK(env.render("", data) == "");
Expand All @@ -39,6 +40,11 @@ TEST_CASE("types") {
CHECK(env.render("{{ @name }}", data) == "@name");
CHECK(env.render("{{ $name }}", data) == "$name");

CHECK(env.render(R"EOF({"Value":"{{ quoted }}"})EOF", data) == R"EOF({"Value":""quoted value""})EOF");
env.set_escape_strings(true);
CHECK(env.render(R"EOF({"Value":{{ quoted }}})EOF", data) == R"EOF({"Value":"\"quoted value\""})EOF");
env.set_escape_strings(false);

CHECK_THROWS_WITH(env.render("{{unknown}}", data), "[inja.exception.render_error] (at 1:3) variable 'unknown' not found");
}

Expand Down

0 comments on commit 0d15c31

Please sign in to comment.