26 TemplateStorage template_storage;
33 std::filesystem::path input_path;
34 std::filesystem::path output_path;
38 explicit Environment(
const std::filesystem::path& global_path): input_path(global_path), output_path(global_path) {}
39 Environment(
const std::filesystem::path& input_path,
const std::filesystem::path& output_path): input_path(input_path), output_path(output_path) {}
43 lexer_config.statement_open = open;
44 lexer_config.statement_open_no_lstrip = open +
"+";
45 lexer_config.statement_open_force_lstrip = open +
"-";
46 lexer_config.statement_close = close;
47 lexer_config.statement_close_force_rstrip =
"-" + close;
48 lexer_config.update_open_chars();
53 lexer_config.line_statement = open;
54 lexer_config.update_open_chars();
59 lexer_config.expression_open = open;
60 lexer_config.expression_open_force_lstrip = open +
"-";
61 lexer_config.expression_close = close;
62 lexer_config.expression_close_force_rstrip =
"-" + close;
63 lexer_config.update_open_chars();
67 void set_comment(
const std::string& open,
const std::string& close) {
68 lexer_config.comment_open = open;
69 lexer_config.comment_open_force_lstrip = open +
"-";
70 lexer_config.comment_close = close;
71 lexer_config.comment_close_force_rstrip =
"-" + close;
72 lexer_config.update_open_chars();
77 lexer_config.trim_blocks = trim_blocks;
82 lexer_config.lstrip_blocks = lstrip_blocks;
87 parser_config.search_included_templates_in_files = search_in_files;
92 render_config.throw_at_missing_includes = will_throw;
97 render_config.html_autoescape = will_escape;
100 Template parse(std::string_view input) {
101 Parser parser(parser_config, lexer_config, template_storage, function_storage);
102 return parser.parse(input, input_path);
105 Template parse_template(
const std::filesystem::path& filename) {
106 Parser parser(parser_config, lexer_config, template_storage, function_storage);
107 auto result = Template(Parser::load_file(input_path / filename));
108 parser.parse_into_template(result, (input_path / filename).
string());
112 Template parse_file(
const std::filesystem::path& filename) {
113 return parse_template(filename);
116 std::string render(std::string_view input,
const json& data) {
117 return render(parse(input), data);
120 std::string render(
const Template& tmpl,
const json& data) {
121 std::stringstream os;
122 render_to(os, tmpl, data);
126 std::string render_file(
const std::filesystem::path& filename,
const json& data) {
127 return render(parse_template(filename), data);
130 std::string render_file_with_json_file(
const std::filesystem::path& filename,
const std::string& filename_data) {
131 const json data = load_json(filename_data);
132 return render_file(filename, data);
135 void write(
const std::filesystem::path& filename,
const json& data,
const std::string& filename_out) {
136 std::ofstream file(output_path / filename_out);
137 file << render_file(filename, data);
141 void write(
const Template& temp,
const json& data,
const std::string& filename_out) {
142 std::ofstream file(output_path / filename_out);
143 file << render(temp, data);
147 void write_with_json_file(
const std::filesystem::path& filename,
const std::string& filename_data,
const std::string& filename_out) {
148 const json data = load_json(filename_data);
149 write(filename, data, filename_out);
152 void write_with_json_file(
const Template& temp,
const std::string& filename_data,
const std::string& filename_out) {
153 const json data = load_json(filename_data);
154 write(temp, data, filename_out);
157 std::ostream& render_to(std::ostream& os,
const Template& tmpl,
const json& data) {
158 Renderer(render_config, template_storage, function_storage).render_to(os, tmpl, data);
162 std::ostream& render_to(std::ostream& os,
const std::string_view input,
const json& data) {
163 return render_to(os, parse(input), data);
166 std::string load_file(
const std::string& filename) {
167 const Parser parser(parser_config, lexer_config, template_storage, function_storage);
168 return Parser::load_file(input_path / filename);
171 json load_json(
const std::string& filename) {
173 file.open(input_path / filename);
175 INJA_THROW(FileError(
"failed accessing file at '" + (input_path / filename).
string() +
"'"));
178 return json::parse(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
184 void add_callback(
const std::string& name,
const CallbackFunction& callback) {
198 void add_callback(
const std::string& name,
int num_args,
const CallbackFunction& callback) {
199 function_storage.add_callback(name, num_args, callback);
205 void add_void_callback(
const std::string& name,
int num_args,
const VoidCallbackFunction& callback) {
206 function_storage.add_callback(name, num_args, [callback](Arguments& args) {
217 template_storage[name] = tmpl;
224 parser_config.include_callback = callback;