diff --git a/include/inja/parser.hpp b/include/inja/parser.hpp index 3cf8de4..3a85181 100644 --- a/include/inja/parser.hpp +++ b/include/inja/parser.hpp @@ -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(path); - pathname += json_name.get_ref(); - 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(); + if (config.search_included_templates_in_files && template_storage.find(template_name) == template_storage.end()) { + // Build the relative path + template_name = static_cast(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(pathname, tok.text.data() - tmpl.content.c_str())); + current_block->nodes.emplace_back(std::make_shared(template_name, tok.text.data() - tmpl.content.c_str())); get_next_token(); diff --git a/test/data/include-both.txt b/test/data/include-both.txt new file mode 100755 index 0000000..cbdc374 --- /dev/null +++ b/test/data/include-both.txt @@ -0,0 +1 @@ +{% include "simple.txt" %} - {% include "body" %} \ No newline at end of file diff --git a/test/test-files.cpp b/test/test-files.cpp index 74d099a..17a0055 100644 --- a/test/test-files.cpp +++ b/test/test-files.cpp @@ -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."); } diff --git a/test/test-functions.cpp b/test/test-functions.cpp index 0f9b6c8..0ddcbf0 100644 --- a/test/test-functions.cpp +++ b/test/test-functions.cpp @@ -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"); }