Skip to content

Commit

Permalink
add config throw_missing_includes, search_in_files
Browse files Browse the repository at this point in the history
  • Loading branch information
pantor committed Jun 27, 2020
1 parent 390db9f commit dadbb85
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 38 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,22 @@ render("{% if not guest_count %}…{% endif %}", data); // True

#### Includes

You can either include other template files or already parsed templates.
You can either include other in-memory templates or from the file system.
```.cpp
// Other template files are included relative from the current file location
render("{% include \"footer.html\" %}", data);

// To include in-memory templates, add them to the environment first
inja::Template content_template = env.parse("Hello {{ neighbour }}!");
env.include_template("content", content_template);
env.render("Content: {% include \"content\" %}", data); // "Content: Hello Peter!"

// Other template files are included relative from the current file location
render("{% include \"footer.html\" %}", data);

// You can disable to search for templates in the file system via
env.set_search_included_templates_in_files(false);
```
Inja will throw an `inja::RenderError` if an included file is not found.
### Functions
A few functions are implemented within the inja template syntax. They can be called with
Expand Down
8 changes: 8 additions & 0 deletions include/inja/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ struct LexerConfig {
*/
struct ParserConfig {
ElementNotation notation {ElementNotation::Dot};
bool search_included_templates_in_files {true};
};

/*!
* \brief Class for render configuration.
*/
struct RenderConfig {
bool throw_at_missing_includes {true};
};

} // namespace inja
Expand Down
32 changes: 21 additions & 11 deletions include/inja/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ using json = nlohmann::json;
* \brief Class for changing the configuration.
*/
class Environment {
std::string input_path;
std::string output_path;

LexerConfig lexer_config;
ParserConfig parser_config;
RenderConfig render_config;

FunctionStorage function_storage;
TemplateStorage template_storage;

public:
Environment() : Environment("") {}

Expand Down Expand Up @@ -76,6 +86,16 @@ class Environment {
parser_config.notation = notation;
}

/// 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;
}

/// Sets whether a missing include will throw an error
void set_throw_at_missing_includes(bool will_throw) {
render_config.throw_at_missing_includes = will_throw;
}

Template parse(nonstd::string_view input) {
Parser parser(parser_config, lexer_config, template_storage);
return parser.parse(input);
Expand Down Expand Up @@ -127,7 +147,7 @@ class Environment {
}

std::ostream &render_to(std::ostream &os, const Template &tmpl, const json &data) {
Renderer(template_storage, function_storage).render_to(os, tmpl, data);
Renderer(render_config, template_storage, function_storage).render_to(os, tmpl, data);
return os;
}

Expand All @@ -154,16 +174,6 @@ class Environment {
void include_template(const std::string &name, const Template &tmpl) {
template_storage[name] = tmpl;
}

private:
std::string input_path;
std::string output_path;

LexerConfig lexer_config;
ParserConfig parser_config;

FunctionStorage function_storage;
TemplateStorage template_storage;
};

/*!
Expand Down
2 changes: 1 addition & 1 deletion include/inja/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ class Parser {
}
// sys::path::remove_dots(pathname, true, sys::path::Style::posix);

if (template_storage.find(pathname) == template_storage.end()) {
if (config.search_included_templates_in_files && template_storage.find(pathname) == template_storage.end()) {
Template include_template = parse_template(pathname);
template_storage.emplace(pathname, include_template);
}
Expand Down
17 changes: 12 additions & 5 deletions include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <nlohmann/json.hpp>

#include "config.hpp"
#include "exceptions.hpp"
#include "node.hpp"
#include "template.hpp"
Expand Down Expand Up @@ -176,9 +177,11 @@ class Renderer {
std::vector<const json *> m_tmp_args;
json m_tmp_val;

RenderConfig config;

public:
Renderer(const TemplateStorage &included_templates, const FunctionStorage &callbacks)
: template_storage(included_templates), function_storage(callbacks) {
Renderer(const RenderConfig& config, const TemplateStorage &included_templates, const FunctionStorage &callbacks)
: config(config), template_storage(included_templates), function_storage(callbacks) {
m_stack.reserve(16);
m_tmp_args.reserve(4);
m_loop_stack.reserve(16);
Expand Down Expand Up @@ -471,10 +474,14 @@ class Renderer {
break;
}
case Node::Op::Include: {
auto sub_renderer = Renderer(template_storage, function_storage);
auto sub_renderer = Renderer(config, template_storage, function_storage);
auto include_name = get_imm(node)->get_ref<const std::string &>();
auto included_template = template_storage.find(include_name)->second;
sub_renderer.render_to(os, included_template, *m_data, m_loop_data);
auto included_template_it = template_storage.find(include_name);
if (included_template_it != template_storage.end()) {
sub_renderer.render_to(os, included_template_it->second, *m_data, m_loop_data);
} else if (config.throw_at_missing_includes) {
throw_renderer_error("include '" + include_name + "' not found", node);
}
break;
}
case Node::Op::Callback: {
Expand Down
60 changes: 43 additions & 17 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,14 @@ struct LexerConfig {
*/
struct ParserConfig {
ElementNotation notation {ElementNotation::Dot};
bool search_included_templates_in_files {true};
};

/*!
* \brief Class for render configuration.
*/
struct RenderConfig {
bool throw_at_missing_includes {true};
};

} // namespace inja
Expand Down Expand Up @@ -2760,7 +2768,7 @@ class Parser {
}
// sys::path::remove_dots(pathname, true, sys::path::Style::posix);

if (template_storage.find(pathname) == template_storage.end()) {
if (config.search_included_templates_in_files && template_storage.find(pathname) == template_storage.end()) {
Template include_template = parse_template(pathname);
template_storage.emplace(pathname, include_template);
}
Expand Down Expand Up @@ -2910,6 +2918,8 @@ class Parser {

#include <nlohmann/json.hpp>

// #include "config.hpp"

// #include "exceptions.hpp"

// #include "node.hpp"
Expand Down Expand Up @@ -3079,9 +3089,11 @@ class Renderer {
std::vector<const json *> m_tmp_args;
json m_tmp_val;

RenderConfig config;

public:
Renderer(const TemplateStorage &included_templates, const FunctionStorage &callbacks)
: template_storage(included_templates), function_storage(callbacks) {
Renderer(const RenderConfig& config, const TemplateStorage &included_templates, const FunctionStorage &callbacks)
: config(config), template_storage(included_templates), function_storage(callbacks) {
m_stack.reserve(16);
m_tmp_args.reserve(4);
m_loop_stack.reserve(16);
Expand Down Expand Up @@ -3374,10 +3386,14 @@ class Renderer {
break;
}
case Node::Op::Include: {
auto sub_renderer = Renderer(template_storage, function_storage);
auto sub_renderer = Renderer(config, template_storage, function_storage);
auto include_name = get_imm(node)->get_ref<const std::string &>();
auto included_template = template_storage.find(include_name)->second;
sub_renderer.render_to(os, included_template, *m_data, m_loop_data);
auto included_template_it = template_storage.find(include_name);
if (included_template_it != template_storage.end()) {
sub_renderer.render_to(os, included_template_it->second, *m_data, m_loop_data);
} else if (config.throw_at_missing_includes) {
throw_renderer_error("include '" + include_name + "' not found", node);
}
break;
}
case Node::Op::Callback: {
Expand Down Expand Up @@ -3521,6 +3537,16 @@ using json = nlohmann::json;
* \brief Class for changing the configuration.
*/
class Environment {
std::string input_path;
std::string output_path;

LexerConfig lexer_config;
ParserConfig parser_config;
RenderConfig render_config;

FunctionStorage function_storage;
TemplateStorage template_storage;

public:
Environment() : Environment("") {}

Expand Down Expand Up @@ -3571,6 +3597,16 @@ class Environment {
parser_config.notation = notation;
}

/// 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;
}

/// Sets whether a missing include will throw an error
void set_throw_at_missing_includes(bool will_throw) {
render_config.throw_at_missing_includes = will_throw;
}

Template parse(nonstd::string_view input) {
Parser parser(parser_config, lexer_config, template_storage);
return parser.parse(input);
Expand Down Expand Up @@ -3622,7 +3658,7 @@ class Environment {
}

std::ostream &render_to(std::ostream &os, const Template &tmpl, const json &data) {
Renderer(template_storage, function_storage).render_to(os, tmpl, data);
Renderer(render_config, template_storage, function_storage).render_to(os, tmpl, data);
return os;
}

Expand All @@ -3649,16 +3685,6 @@ class Environment {
void include_template(const std::string &name, const Template &tmpl) {
template_storage[name] = tmpl;
}

private:
std::string input_path;
std::string output_path;

LexerConfig lexer_config;
ParserConfig parser_config;

FunctionStorage function_storage;
TemplateStorage template_storage;
};

/*!
Expand Down
9 changes: 9 additions & 0 deletions test/unit-files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,12 @@ TEST_CASE("global-path") {
CHECK(env_result.load_file("global-path-result.txt") == "Hello Jeff.");
}
}

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 21:1) include '../test/data/html/header.txt' not found");
}
}

0 comments on commit dadbb85

Please sign in to comment.