Skip to content

Commit

Permalink
stow the created template on the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jbohanon committed Mar 14, 2024
1 parent 3aa95b8 commit c3cc578
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion include/inja/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Parser {

Lexer lexer;
TemplateStorage& template_storage;
std::vector<Template> template_stash;
const FunctionStorage& function_storage;

Token tok, peek_tok;
Expand Down Expand Up @@ -622,7 +623,7 @@ class Parser {
: config(parser_config), lexer(lexer_config), template_storage(template_storage), function_storage(function_storage) {}

Template parse(std::string_view input, std::string_view path) {
auto result = Template(static_cast<std::string>(input));
auto& result = template_stash.emplace_back(Template(static_cast<std::string>(input)));
parse_into(result, path);
return result;
}
Expand Down
7 changes: 6 additions & 1 deletion single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,7 @@ class Parser {

Lexer lexer;
TemplateStorage& template_storage;
std::vector<Template> template_stash;
const FunctionStorage& function_storage;

Token tok, peek_tok;
Expand Down Expand Up @@ -2060,7 +2061,7 @@ class Parser {
: config(parser_config), lexer(lexer_config), template_storage(template_storage), function_storage(function_storage) {}

Template parse(std::string_view input, std::string_view path) {
auto result = Template(static_cast<std::string>(input));
auto& result = template_stash.emplace_back(Template(static_cast<std::string>(input)));
parse_into(result, path);
return result;
}
Expand Down Expand Up @@ -2153,7 +2154,11 @@ class Renderer : public NodeVisitor {
if (value->is_string()) {
std::string val;
if (config.escape_strings) {
// get the value as a dump() to properly escape values
val = value->dump();

// strip the leading and trailing " characters that are added by dump()
// if C++20 is adopted, val.starts_with and val.ends_with would clean this up a bit
val = val.substr(0,1) == "\"" && val.substr(val.length()-1,1) == "\""
? val.substr(1, val.length()-2)
: val;
Expand Down

0 comments on commit c3cc578

Please sign in to comment.