fix include of in-memory templates

This commit is contained in:
pantor
2021-05-17 20:44:32 +02:00
parent ca3c7a0cd6
commit eac2162629
4 changed files with 30 additions and 16 deletions

View File

@@ -449,22 +449,22 @@ class Parser {
throw_parser_error("expected string, got '" + tok.describe() + "'");
}
// Build the relative path
json json_name = json::parse(tok.text);
std::string pathname = static_cast<std::string>(path);
pathname += json_name.get_ref<const std::string &>();
if (pathname.compare(0, 2, "./") == 0) {
pathname.erase(0, 2);
}
// sys::path::remove_dots(pathname, true, sys::path::Style::posix);
std::string template_name = json::parse(tok.text).get_ref<const std::string &>();
if (config.search_included_templates_in_files && template_storage.find(template_name) == template_storage.end()) {
// Build the relative path
template_name = static_cast<std::string>(path) + template_name;
if (template_name.compare(0, 2, "./") == 0) {
template_name.erase(0, 2);
}
if (config.search_included_templates_in_files && template_storage.find(pathname) == template_storage.end()) {
auto include_template = Template(load_file(pathname));
template_storage.emplace(pathname, include_template);
parse_into_template(template_storage[pathname], pathname);
if (template_storage.find(template_name) == template_storage.end()) {
auto include_template = Template(load_file(template_name));
template_storage.emplace(template_name, include_template);
parse_into_template(template_storage[template_name], template_name);
}
}
current_block->nodes.emplace_back(std::make_shared<IncludeStatementNode>(pathname, tok.text.data() - tmpl.content.c_str()));
current_block->nodes.emplace_back(std::make_shared<IncludeStatementNode>(template_name, tok.text.data() - tmpl.content.c_str()));
get_next_token();

1
test/data/include-both.txt Executable file
View File

@@ -0,0 +1 @@
{% include "simple.txt" %} - {% include "body" %}

View File

@@ -73,7 +73,19 @@ TEST_CASE("include-without-local-files") {
inja::Environment env {test_file_directory};
env.set_search_included_templates_in_files(false);
SUBCASE("html") {
CHECK_THROWS_WITH(env.render_file_with_json_file("html/template.txt", "html/data.json"), "[inja.exception.render_error] (at 3:14) include '../test/data/html/header.txt' not found");
}
CHECK_THROWS_WITH(env.render_file_with_json_file("html/template.txt", "html/data.json"), "[inja.exception.render_error] (at 3:14) include 'header.txt' not found");
}
TEST_CASE("include-in-memory-and-file-template") {
inja::Environment env {test_file_directory};
json data;
data["name"] = "Jeff";
CHECK_THROWS_WITH(env.render_file("include-both.txt", data), "[inja.exception.file_error] failed accessing file at '../test/data/body'");
const auto parsed_body_template = env.parse("Bye {{ name }}.");
env.include_template("body", parsed_body_template);
CHECK(env.render_file("include-both.txt", data) == "Hello Jeff. - Bye Jeff.");
}

View File

@@ -282,4 +282,5 @@ TEST_CASE("combinations") {
CHECK(env.render("{{ not true }}", data) == "false");
CHECK(env.render("{{ not (true) }}", data) == "false");
CHECK(env.render("{{ true or (true or true) }}", data) == "true");
CHECK(env.render("{{ at(list_of_objects, 1).b }}", data) == "3");
}