mirror of
https://github.com/pantor/inja.git
synced 2026-02-17 09:03:58 +00:00
fix include of in-memory templates
This commit is contained in:
@@ -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
1
test/data/include-both.txt
Executable file
@@ -0,0 +1 @@
|
||||
{% include "simple.txt" %} - {% include "body" %}
|
||||
@@ -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.");
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user