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::string &global_path)
 Environment (const std::string &input_path, const std::string &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.
Template parse (std::string_view input)
Template parse_template (const std::string &filename)
Template parse_file (const std::string &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::string &filename, const json &data)
std::string render_file_with_json_file (const std::string &filename, const std::string &filename_data)
void write (const std::string &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::string &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::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::string &, const std::string &)> &callback)
 Sets a function that is called when an included file is not found.

Protected Attributes

std::string input_path
std::string output_path

Private Attributes

LexerConfig lexer_config
ParserConfig parser_config
RenderConfig render_config
FunctionStorage function_storage
TemplateStorage template_storage

Detailed Description

Class for changing the configuration.

Definition at line 2851 of file inja.hpp.

Constructor & Destructor Documentation

◆ Environment() [1/3]

inja::Environment::Environment ( )
inline

Definition at line 2864 of file inja.hpp.

2865 : Environment("") {
2866 }

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::string & global_path)
inlineexplicit

Definition at line 2868 of file inja.hpp.

2869 : input_path(global_path)
2870 , output_path(global_path) {
2871 }
std::string input_path
Definition inja.hpp:2860
std::string output_path
Definition inja.hpp:2861

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::string & input_path,
const std::string & output_path )
inline

Definition at line 2873 of file inja.hpp.

2876 }

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 3012 of file inja.hpp.

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

◆ 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 3026 of file inja.hpp.

3026 {
3027 function_storage.add_callback(name, num_args, callback);
3028 }
FunctionStorage function_storage
Definition inja.hpp:2856

References function_storage.

◆ 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 3019 of file inja.hpp.

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

◆ 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 3033 of file inja.hpp.

3033 {
3034 function_storage.add_callback(name, num_args, [callback](Arguments& args) {
3035 callback(args);
3036 return json();
3037 });
3038 }
std::vector< const nlohmann::json * > Arguments
nlohmann::json json

References function_storage.

◆ 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 3044 of file inja.hpp.

3044 {
3045 template_storage[name] = tmpl;
3046 }
TemplateStorage template_storage
Definition inja.hpp:2857

References template_storage.

◆ load_file()

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

Definition at line 2994 of file inja.hpp.

2994 {
2996 return parser.load_file(input_path + filename);
2997 }
LexerConfig lexer_config
Definition inja.hpp:2852
ParserConfig parser_config
Definition inja.hpp:2853

References function_storage, input_path, lexer_config, inja::Parser::load_file(), 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 2999 of file inja.hpp.

2999 {
3000 std::ifstream file;
3001 file.open(input_path + filename);
3002 if (file.fail()) {
3003 INJA_THROW(FileError("failed accessing file at '" + input_path + filename + "'"));
3004 }
3005
3006 return json::parse(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
3007 }
#define INJA_THROW(exception)
Definition inja.hpp:45

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 2932 of file inja.hpp.

2932 {
2934 return parser.parse(input, input_path);
2935 }

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

Referenced by render(), 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::string & filename)
inline

Definition at line 2944 of file inja.hpp.

2944 {
2945 return parse_template(filename);
2946 }
Template parse_template(const std::string &filename)
Definition inja.hpp:2937

References parse_template().

Here is the call graph for this function:

◆ parse_template()

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

Definition at line 2937 of file inja.hpp.

2937 {
2939 auto result = Template(parser.load_file(input_path + static_cast<std::string>(filename)));
2940 parser.parse_into_template(result, input_path + static_cast<std::string>(filename));
2941 return result;
2942 }

References function_storage, input_path, lexer_config, inja::Parser::load_file(), inja::Parser::parse_into_template(), 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 2952 of file inja.hpp.

2952 {
2953 std::stringstream os;
2954 render_to(os, tmpl, data);
2955 return os.str();
2956 }
std::ostream & render_to(std::ostream &os, const Template &tmpl, const json &data)
Definition inja.hpp:2989

◆ render() [2/2]

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

Definition at line 2948 of file inja.hpp.

2948 {
2949 return render(parse(input), data);
2950 }
Template parse(std::string_view input)
Definition inja.hpp:2932
std::string render(std::string_view input, const json &data)
Definition inja.hpp:2948

References parse().

Here is the call graph for this function:

◆ render_file()

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

Definition at line 2958 of file inja.hpp.

2958 {
2959 return render(parse_template(filename), data);
2960 }

References parse_template().

Here is the call graph for this function:

◆ render_file_with_json_file()

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

Definition at line 2962 of file inja.hpp.

2962 {
2963 const json data = load_json(filename_data);
2964 return render_file(filename, data);
2965 }
json load_json(const std::string &filename)
Definition inja.hpp:2999
std::string render_file(const std::string &filename, const json &data)
Definition inja.hpp:2958

References load_json().

Here is the call graph for this function:

◆ render_to()

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

Definition at line 2989 of file inja.hpp.

2989 {
2990 Renderer(render_config, template_storage, function_storage).render_to(os, tmpl, data);
2991 return os;
2992 }
RenderConfig render_config
Definition inja.hpp:2854

References function_storage, render_config, and template_storage.

◆ 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 2904 of file inja.hpp.

2904 {
2905 lexer_config.comment_open = open;
2906 lexer_config.comment_open_force_lstrip = open + "-";
2907 lexer_config.comment_close = close;
2908 lexer_config.comment_close_force_rstrip = "-" + close;
2909 lexer_config.update_open_chars();
2910 }

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 2895 of file inja.hpp.

2895 {
2896 lexer_config.expression_open = open;
2897 lexer_config.expression_open_force_lstrip = open + "-";
2898 lexer_config.expression_close = close;
2899 lexer_config.expression_close_force_rstrip = "-" + close;
2900 lexer_config.update_open_chars();
2901 }

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_include_callback()

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

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

Definition at line 3051 of file inja.hpp.

3051 {
3052 parser_config.include_callback = callback;
3053 }

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 2889 of file inja.hpp.

2889 {
2890 lexer_config.line_statement = open;
2891 lexer_config.update_open_chars();
2892 }

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 2918 of file inja.hpp.

2918 {
2919 lexer_config.lstrip_blocks = lstrip_blocks;
2920 }

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 2923 of file inja.hpp.

2923 {
2924 parser_config.search_included_templates_in_files = search_in_files;
2925 }

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 2879 of file inja.hpp.

2879 {
2880 lexer_config.statement_open = open;
2881 lexer_config.statement_open_no_lstrip = open + "+";
2882 lexer_config.statement_open_force_lstrip = open + "-";
2883 lexer_config.statement_close = close;
2884 lexer_config.statement_close_force_rstrip = "-" + close;
2885 lexer_config.update_open_chars();
2886 }

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 2928 of file inja.hpp.

2928 {
2929 render_config.throw_at_missing_includes = will_throw;
2930 }

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 2913 of file inja.hpp.

2913 {
2914 lexer_config.trim_blocks = trim_blocks;
2915 }

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

◆ write() [1/2]

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

Definition at line 2967 of file inja.hpp.

2967 {
2968 std::ofstream file(output_path + filename_out);
2969 file << render_file(filename, data);
2970 file.close();
2971 }

References output_path.

◆ write() [2/2]

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

Definition at line 2973 of file inja.hpp.

2973 {
2974 std::ofstream file(output_path + filename_out);
2975 file << render(temp, data);
2976 file.close();
2977 }

References output_path.

◆ write_with_json_file() [1/2]

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

Definition at line 2979 of file inja.hpp.

2979 {
2980 const json data = load_json(filename_data);
2981 write(filename, data, filename_out);
2982 }
void write(const std::string &filename, const json &data, const std::string &filename_out)
Definition inja.hpp:2967

References load_json().

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 2984 of file inja.hpp.

2984 {
2985 const json data = load_json(filename_data);
2986 write(temp, data, filename_out);
2987 }

References load_json().

Here is the call graph for this function:

Member Data Documentation

◆ function_storage

FunctionStorage inja::Environment::function_storage
private

Definition at line 2856 of file inja.hpp.

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

◆ input_path

std::string inja::Environment::input_path
protected

Definition at line 2860 of file inja.hpp.

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

◆ lexer_config

LexerConfig inja::Environment::lexer_config
private

◆ output_path

std::string inja::Environment::output_path
protected

Definition at line 2861 of file inja.hpp.

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

◆ parser_config

ParserConfig inja::Environment::parser_config
private

◆ render_config

RenderConfig inja::Environment::render_config
private

Definition at line 2854 of file inja.hpp.

Referenced by render_to(), and set_throw_at_missing_includes().

◆ template_storage

TemplateStorage inja::Environment::template_storage
private

Definition at line 2857 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: