MQTTSuite
Loading...
Searching...
No Matches
inja::Environment Class Reference

Class for changing the configuration. More...

#include <inja.hpp>

Collaboration diagram for inja::Environment:

Public Member Functions

 Environment ()
 Environment (const std::filesystem::path &global_path)
 Environment (const std::filesystem::path &input_path, const std::filesystem::path &output_path)
void set_statement (const std::string &open, const std::string &close)
 Sets the opener and closer for template statements.
void set_line_statement (const std::string &open)
 Sets the opener for template line statements.
void set_expression (const std::string &open, const std::string &close)
 Sets the opener and closer for template expressions.
void set_comment (const std::string &open, const std::string &close)
 Sets the opener and closer for template comments.
void set_trim_blocks (bool trim_blocks)
 Sets whether to remove the first newline after a block.
void set_lstrip_blocks (bool lstrip_blocks)
 Sets whether to strip the spaces and tabs from the start of a line to a block.
void set_search_included_templates_in_files (bool search_in_files)
 Sets the element notation syntax.
void set_throw_at_missing_includes (bool will_throw)
 Sets whether a missing include will throw an error.
void set_html_autoescape (bool will_escape)
 Sets whether we'll automatically perform HTML escape.
Template parse (std::string_view input)
Template parse_template (const std::filesystem::path &filename)
Template parse_file (const std::filesystem::path &filename)
std::string render (std::string_view input, const json &data)
std::string render (const Template &tmpl, const json &data)
std::string render_file (const std::filesystem::path &filename, const json &data)
std::string render_file_with_json_file (const std::filesystem::path &filename, const std::string &filename_data)
void write (const std::filesystem::path &filename, const json &data, const std::string &filename_out)
void write (const Template &temp, const json &data, const std::string &filename_out)
void write_with_json_file (const std::filesystem::path &filename, const std::string &filename_data, const std::string &filename_out)
void write_with_json_file (const Template &temp, const std::string &filename_data, const std::string &filename_out)
std::ostream & render_to (std::ostream &os, const Template &tmpl, const json &data)
std::ostream & render_to (std::ostream &os, const std::string_view input, const json &data)
std::string load_file (const std::string &filename)
json load_json (const std::string &filename)
void add_callback (const std::string &name, const CallbackFunction &callback)
 Adds a variadic callback.
void add_void_callback (const std::string &name, const VoidCallbackFunction &callback)
 Adds a variadic void callback.
void add_callback (const std::string &name, int num_args, const CallbackFunction &callback)
 Adds a callback with given number or arguments.
void add_void_callback (const std::string &name, int num_args, const VoidCallbackFunction &callback)
 Adds a void callback with given number or arguments.
void include_template (const std::string &name, const Template &tmpl)
void set_include_callback (const std::function< Template(const std::filesystem::path &, const std::string &)> &callback)
 Sets a function that is called when an included file is not found.

Protected Attributes

LexerConfig lexer_config
ParserConfig parser_config
RenderConfig render_config
std::filesystem::path input_path
std::filesystem::path output_path

Private Attributes

FunctionStorage function_storage
TemplateStorage template_storage

Detailed Description

Class for changing the configuration.

Definition at line 2993 of file inja.hpp.

Constructor & Destructor Documentation

◆ Environment() [1/3]

inja::Environment::Environment ( )
inline

Definition at line 3006 of file inja.hpp.

3007 : Environment("") {
3008 }

References Environment().

Referenced by inja::render().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Environment() [2/3]

inja::Environment::Environment ( const std::filesystem::path & global_path)
inlineexplicit

Definition at line 3009 of file inja.hpp.

3010 : input_path(global_path)
3011 , output_path(global_path) {
3012 }
std::filesystem::path output_path
Definition inja.hpp:3003
std::filesystem::path input_path
Definition inja.hpp:3002

References input_path, and output_path.

Referenced by Environment().

Here is the caller graph for this function:

◆ Environment() [3/3]

inja::Environment::Environment ( const std::filesystem::path & input_path,
const std::filesystem::path & output_path )
inline

Definition at line 3013 of file inja.hpp.

3016 }

References input_path, and output_path.

Member Function Documentation

◆ add_callback() [1/2]

void inja::Environment::add_callback ( const std::string & name,
const CallbackFunction & callback )
inline

Adds a variadic callback.

Definition at line 3162 of file inja.hpp.

3162 {
3163 add_callback(name, -1, callback);
3164 }
void add_callback(const std::string &name, const CallbackFunction &callback)
Adds a variadic callback.
Definition inja.hpp:3162

References add_callback().

Here is the call graph for this function:

◆ add_callback() [2/2]

void inja::Environment::add_callback ( const std::string & name,
int num_args,
const CallbackFunction & callback )
inline

Adds a callback with given number or arguments.

Definition at line 3176 of file inja.hpp.

3176 {
3177 function_storage.add_callback(name, num_args, callback);
3178 }
FunctionStorage function_storage
Definition inja.hpp:2994

References inja::FunctionStorage::add_callback(), and function_storage.

Referenced by add_callback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_void_callback() [1/2]

void inja::Environment::add_void_callback ( const std::string & name,
const VoidCallbackFunction & callback )
inline

Adds a variadic void callback.

Definition at line 3169 of file inja.hpp.

3169 {
3170 add_void_callback(name, -1, callback);
3171 }
void add_void_callback(const std::string &name, const VoidCallbackFunction &callback)
Adds a variadic void callback.
Definition inja.hpp:3169

References add_void_callback().

Here is the call graph for this function:

◆ add_void_callback() [2/2]

void inja::Environment::add_void_callback ( const std::string & name,
int num_args,
const VoidCallbackFunction & callback )
inline

Adds a void callback with given number or arguments.

Definition at line 3183 of file inja.hpp.

3183 {
3184 function_storage.add_callback(name, num_args, [callback](Arguments& args) {
3185 callback(args);
3186 return json();
3187 });
3188 }
std::vector< const nlohmann::json * > Arguments
nlohmann::json json

References inja::FunctionStorage::add_callback(), and function_storage.

Referenced by add_void_callback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ include_template()

void inja::Environment::include_template ( const std::string & name,
const Template & tmpl )
inline

Includes a template with a given name into the environment. Then, a template can be rendered in another template using the include "<name>" syntax.

Definition at line 3194 of file inja.hpp.

3194 {
3195 template_storage[name] = tmpl;
3196 }
TemplateStorage template_storage
Definition inja.hpp:2995

References template_storage.

◆ load_file()

std::string inja::Environment::load_file ( const std::string & filename)
inline

Definition at line 3144 of file inja.hpp.

3144 {
3146 return Parser::load_file(input_path / filename);
3147 }
LexerConfig lexer_config
Definition inja.hpp:2998
ParserConfig parser_config
Definition inja.hpp:2999
static std::string load_file(const std::filesystem::path &filename)
Definition inja.hpp:2245

References function_storage, input_path, lexer_config, inja::Parser::load_file(), inja::Parser::Parser(), parser_config, and template_storage.

Here is the call graph for this function:

◆ load_json()

json inja::Environment::load_json ( const std::string & filename)
inline

Definition at line 3149 of file inja.hpp.

3149 {
3150 std::ifstream file;
3151 file.open(input_path / filename);
3152 if (file.fail()) {
3153 INJA_THROW(FileError("failed accessing file at '" + (input_path / filename).string() + "'"));
3154 }
3155
3156 return json::parse(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
3157 }
#define INJA_THROW(exception)
Definition inja.hpp:55

References inja::FileError::FileError(), and input_path.

Referenced by render_file_with_json_file(), write_with_json_file(), and write_with_json_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse()

Template inja::Environment::parse ( std::string_view input)
inline

Definition at line 3077 of file inja.hpp.

3077 {
3079 return parser.parse(input, input_path);
3080 }

References function_storage, input_path, lexer_config, inja::Parser::parse(), inja::Parser::Parser(), parser_config, and template_storage.

Referenced by render(), render_to(), and inja::render_to().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_file()

Template inja::Environment::parse_file ( const std::filesystem::path & filename)
inline

Definition at line 3089 of file inja.hpp.

3089 {
3090 return parse_template(filename);
3091 }
Template parse_template(const std::filesystem::path &filename)
Definition inja.hpp:3082

References parse_template().

Here is the call graph for this function:

◆ parse_template()

Template inja::Environment::parse_template ( const std::filesystem::path & filename)
inline

Definition at line 3082 of file inja.hpp.

3082 {
3084 auto result = Template(Parser::load_file(input_path / filename));
3085 parser.parse_into_template(result, (input_path / filename).string());
3086 return result;
3087 }

References function_storage, input_path, lexer_config, inja::Parser::load_file(), inja::Parser::parse_into_template(), inja::Parser::Parser(), parser_config, inja::Template::Template(), and template_storage.

Referenced by parse_file(), and render_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ render() [1/2]

std::string inja::Environment::render ( const Template & tmpl,
const json & data )
inline

Definition at line 3097 of file inja.hpp.

3097 {
3098 std::stringstream os;
3099 render_to(os, tmpl, data);
3100 return os.str();
3101 }
std::ostream & render_to(std::ostream &os, const Template &tmpl, const json &data)
Definition inja.hpp:3135

References render_to().

Referenced by render(), render_file(), and write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ render() [2/2]

std::string inja::Environment::render ( std::string_view input,
const json & data )
inline

Definition at line 3093 of file inja.hpp.

3093 {
3094 return render(parse(input), data);
3095 }
Template parse(std::string_view input)
Definition inja.hpp:3077
std::string render(std::string_view input, const json &data)
Definition inja.hpp:3093

References parse(), and render().

Referenced by mqtt::lib::MqttMapper::publishMappedTemplate(), and inja::render().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ render_file()

std::string inja::Environment::render_file ( const std::filesystem::path & filename,
const json & data )
inline

Definition at line 3103 of file inja.hpp.

3103 {
3104 return render(parse_template(filename), data);
3105 }

References parse_template(), and render().

Referenced by render_file_with_json_file(), and write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ render_file_with_json_file()

std::string inja::Environment::render_file_with_json_file ( const std::filesystem::path & filename,
const std::string & filename_data )
inline

Definition at line 3107 of file inja.hpp.

3107 {
3108 const json data = load_json(filename_data);
3109 return render_file(filename, data);
3110 }
std::string render_file(const std::filesystem::path &filename, const json &data)
Definition inja.hpp:3103
json load_json(const std::string &filename)
Definition inja.hpp:3149

References load_json(), and render_file().

Here is the call graph for this function:

◆ render_to() [1/2]

std::ostream & inja::Environment::render_to ( std::ostream & os,
const std::string_view input,
const json & data )
inline

Definition at line 3140 of file inja.hpp.

3140 {
3141 return render_to(os, parse(input), data);
3142 }

References parse(), and render_to().

Here is the call graph for this function:

◆ render_to() [2/2]

std::ostream & inja::Environment::render_to ( std::ostream & os,
const Template & tmpl,
const json & data )
inline

Definition at line 3135 of file inja.hpp.

3135 {
3136 Renderer(render_config, template_storage, function_storage).render_to(os, tmpl, data);
3137 return os;
3138 }
RenderConfig render_config
Definition inja.hpp:3000

References function_storage, render_config, inja::Renderer::render_to(), inja::Renderer::Renderer(), and template_storage.

Referenced by render(), render_to(), and inja::render_to().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_comment()

void inja::Environment::set_comment ( const std::string & open,
const std::string & close )
inline

Sets the opener and closer for template comments.

Definition at line 3044 of file inja.hpp.

3044 {
3045 lexer_config.comment_open = open;
3046 lexer_config.comment_open_force_lstrip = open + "-";
3047 lexer_config.comment_close = close;
3048 lexer_config.comment_close_force_rstrip = "-" + close;
3049 lexer_config.update_open_chars();
3050 }

References inja::LexerConfig::comment_close, inja::LexerConfig::comment_close_force_rstrip, inja::LexerConfig::comment_open, inja::LexerConfig::comment_open_force_lstrip, lexer_config, and inja::LexerConfig::update_open_chars().

Here is the call graph for this function:

◆ set_expression()

void inja::Environment::set_expression ( const std::string & open,
const std::string & close )
inline

Sets the opener and closer for template expressions.

Definition at line 3035 of file inja.hpp.

3035 {
3036 lexer_config.expression_open = open;
3037 lexer_config.expression_open_force_lstrip = open + "-";
3038 lexer_config.expression_close = close;
3039 lexer_config.expression_close_force_rstrip = "-" + close;
3040 lexer_config.update_open_chars();
3041 }

References inja::LexerConfig::expression_close, inja::LexerConfig::expression_close_force_rstrip, inja::LexerConfig::expression_open, inja::LexerConfig::expression_open_force_lstrip, lexer_config, and inja::LexerConfig::update_open_chars().

Here is the call graph for this function:

◆ set_html_autoescape()

void inja::Environment::set_html_autoescape ( bool will_escape)
inline

Sets whether we'll automatically perform HTML escape.

Definition at line 3073 of file inja.hpp.

3073 {
3074 render_config.html_autoescape = will_escape;
3075 }

References inja::RenderConfig::html_autoescape, and render_config.

◆ set_include_callback()

void inja::Environment::set_include_callback ( const std::function< Template(const std::filesystem::path &, const std::string &)> & callback)
inline

Sets a function that is called when an included file is not found.

Definition at line 3201 of file inja.hpp.

3201 {
3202 parser_config.include_callback = callback;
3203 }

References inja::ParserConfig::include_callback, and parser_config.

◆ set_line_statement()

void inja::Environment::set_line_statement ( const std::string & open)
inline

Sets the opener for template line statements.

Definition at line 3029 of file inja.hpp.

3029 {
3030 lexer_config.line_statement = open;
3031 lexer_config.update_open_chars();
3032 }

References lexer_config, inja::LexerConfig::line_statement, and inja::LexerConfig::update_open_chars().

Here is the call graph for this function:

◆ set_lstrip_blocks()

void inja::Environment::set_lstrip_blocks ( bool lstrip_blocks)
inline

Sets whether to strip the spaces and tabs from the start of a line to a block.

Definition at line 3058 of file inja.hpp.

3058 {
3059 lexer_config.lstrip_blocks = lstrip_blocks;
3060 }

References lexer_config, and inja::LexerConfig::lstrip_blocks.

◆ set_search_included_templates_in_files()

void inja::Environment::set_search_included_templates_in_files ( bool search_in_files)
inline

Sets the element notation syntax.

Definition at line 3063 of file inja.hpp.

3063 {
3064 parser_config.search_included_templates_in_files = search_in_files;
3065 }

References parser_config, and inja::ParserConfig::search_included_templates_in_files.

◆ set_statement()

void inja::Environment::set_statement ( const std::string & open,
const std::string & close )
inline

Sets the opener and closer for template statements.

Definition at line 3019 of file inja.hpp.

3019 {
3020 lexer_config.statement_open = open;
3021 lexer_config.statement_open_no_lstrip = open + "+";
3022 lexer_config.statement_open_force_lstrip = open + "-";
3023 lexer_config.statement_close = close;
3024 lexer_config.statement_close_force_rstrip = "-" + close;
3025 lexer_config.update_open_chars();
3026 }

References lexer_config, inja::LexerConfig::statement_close, inja::LexerConfig::statement_close_force_rstrip, inja::LexerConfig::statement_open, inja::LexerConfig::statement_open_force_lstrip, inja::LexerConfig::statement_open_no_lstrip, and inja::LexerConfig::update_open_chars().

Here is the call graph for this function:

◆ set_throw_at_missing_includes()

void inja::Environment::set_throw_at_missing_includes ( bool will_throw)
inline

Sets whether a missing include will throw an error.

Definition at line 3068 of file inja.hpp.

3068 {
3069 render_config.throw_at_missing_includes = will_throw;
3070 }

References render_config, and inja::RenderConfig::throw_at_missing_includes.

◆ set_trim_blocks()

void inja::Environment::set_trim_blocks ( bool trim_blocks)
inline

Sets whether to remove the first newline after a block.

Definition at line 3053 of file inja.hpp.

3053 {
3054 lexer_config.trim_blocks = trim_blocks;
3055 }

References lexer_config, and inja::LexerConfig::trim_blocks.

◆ write() [1/2]

void inja::Environment::write ( const std::filesystem::path & filename,
const json & data,
const std::string & filename_out )
inline

Definition at line 3112 of file inja.hpp.

3112 {
3113 std::ofstream file(output_path / filename_out);
3114 file << render_file(filename, data);
3115 file.close();
3116 }

References output_path, and render_file().

Referenced by write_with_json_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write() [2/2]

void inja::Environment::write ( const Template & temp,
const json & data,
const std::string & filename_out )
inline

Definition at line 3118 of file inja.hpp.

3118 {
3119 std::ofstream file(output_path / filename_out);
3120 file << render(temp, data);
3121 file.close();
3122 }

References output_path, and render().

Referenced by write_with_json_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_with_json_file() [1/2]

void inja::Environment::write_with_json_file ( const std::filesystem::path & filename,
const std::string & filename_data,
const std::string & filename_out )
inline

Definition at line 3125 of file inja.hpp.

3125 {
3126 const json data = load_json(filename_data);
3127 write(filename, data, filename_out);
3128 }
void write(const std::filesystem::path &filename, const json &data, const std::string &filename_out)
Definition inja.hpp:3112

References load_json(), and write().

Here is the call graph for this function:

◆ write_with_json_file() [2/2]

void inja::Environment::write_with_json_file ( const Template & temp,
const std::string & filename_data,
const std::string & filename_out )
inline

Definition at line 3130 of file inja.hpp.

3130 {
3131 const json data = load_json(filename_data);
3132 write(temp, data, filename_out);
3133 }

References load_json(), and write().

Here is the call graph for this function:

Member Data Documentation

◆ function_storage

FunctionStorage inja::Environment::function_storage
private

Definition at line 2994 of file inja.hpp.

Referenced by add_callback(), add_void_callback(), load_file(), parse(), parse_template(), and render_to().

◆ input_path

std::filesystem::path inja::Environment::input_path
protected

Definition at line 3002 of file inja.hpp.

Referenced by Environment(), Environment(), load_file(), load_json(), parse(), and parse_template().

◆ lexer_config

LexerConfig inja::Environment::lexer_config
protected

◆ output_path

std::filesystem::path inja::Environment::output_path
protected

Definition at line 3003 of file inja.hpp.

Referenced by Environment(), Environment(), write(), and write().

◆ parser_config

ParserConfig inja::Environment::parser_config
protected

◆ render_config

RenderConfig inja::Environment::render_config
protected

Definition at line 3000 of file inja.hpp.

Referenced by render_to(), set_html_autoescape(), and set_throw_at_missing_includes().

◆ template_storage

TemplateStorage inja::Environment::template_storage
private

Definition at line 2995 of file inja.hpp.

Referenced by include_template(), load_file(), parse(), parse_template(), and render_to().


The documentation for this class was generated from the following file: