From df4379d766c5d31da5a3560c6554ea2ebbb43012 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 26 Feb 2024 21:58:35 +0100 Subject: [PATCH] Added a parameter to HTMLTemplate::process_template_expression_variable() to be able to control when to emit errors if a variable is not set. Now this is used when checking for the first argument for the q template method variants. --- modules/web/doc_classes/HTMLTemplate.xml | 1 + modules/web/html/html_template.cpp | 34 ++++++++++++++++-------- modules/web/html/html_template.h | 2 +- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/modules/web/doc_classes/HTMLTemplate.xml b/modules/web/doc_classes/HTMLTemplate.xml index 3d277aeb0..02553cded 100644 --- a/modules/web/doc_classes/HTMLTemplate.xml +++ b/modules/web/doc_classes/HTMLTemplate.xml @@ -158,6 +158,7 @@ + Processes one variable from an expression in a template. An expression in the template look like: [code]{{ EXPRESSION }}[/code]. diff --git a/modules/web/html/html_template.cpp b/modules/web/html/html_template.cpp index 5bb3fc271..22954276f 100644 --- a/modules/web/html/html_template.cpp +++ b/modules/web/html/html_template.cpp @@ -229,14 +229,14 @@ String HTMLTemplate::get_template_text(const StringName &p_name) { String HTMLTemplate::call_template_method(const TemplateExpressionMethods p_method, const Array &p_data, const bool p_first_var_decides_print) { int s = p_data.size(); - + if (s == 0) { return String(); } - + if (p_first_var_decides_print) { Variant v = p_data[0]; - + if (!v) { return String(); } @@ -244,11 +244,11 @@ String HTMLTemplate::call_template_method(const TemplateExpressionMethods p_meth if (p_method != TEMPLATE_EXPRESSION_METHOD_VFORMAT) { int arg_start = 0; - + if (p_first_var_decides_print) { ++arg_start; } - + String ret; for (int i = arg_start; i < s; ++i) { @@ -273,11 +273,11 @@ String HTMLTemplate::call_template_method(const TemplateExpressionMethods p_meth return ret; } else { int arg_start = 1; - + if (p_first_var_decides_print) { ++arg_start; } - + //VFormat ERR_FAIL_COND_V_MSG(s < arg_start, String(), "vformat requires at least one positional argument!"); @@ -301,7 +301,7 @@ String HTMLTemplate::call_template_method(const TemplateExpressionMethods p_meth return String(); } -Variant HTMLTemplate::process_template_expression_variable(const String &p_variable, const Dictionary &p_data) { +Variant HTMLTemplate::process_template_expression_variable(const String &p_variable, const Dictionary &p_data, const bool p_allow_missing) { // "XXX" // String // 'XXX' // String // var[1] // Array indexing @@ -334,6 +334,10 @@ Variant HTMLTemplate::process_template_expression_variable(const String &p_varia const Variant *element = p_data.getptr(Variant(p_variable)); + if (p_allow_missing && !element) { + return Variant(); + } + ERR_FAIL_COND_V_MSG(!element, Variant(), "The given Dictionary does not contain value! " + variable); return *element; @@ -341,10 +345,14 @@ Variant HTMLTemplate::process_template_expression_variable(const String &p_varia int rsqbrace_pos = variable.find_last("]"); - // Has no [, but has ]. Might be a bug, or just ] in name of variable. If it's not intenrional, the error macro will get triggered. + // Has no [, but has ]. Might be a bug, or just ] in name of variable. If it's not intentional, the error macro will get triggered. if (rsqbrace_pos == -1) { const Variant *element = p_data.getptr(Variant(p_variable)); + if (p_allow_missing && !element) { + return Variant(); + } + ERR_FAIL_COND_V_MSG(!element, Variant(), "The given Dictionary does not contain value! " + variable); return *element; @@ -354,6 +362,10 @@ Variant HTMLTemplate::process_template_expression_variable(const String &p_varia const Variant *element = p_data.getptr(Variant(var_name)); + if (p_allow_missing && !element) { + return Variant(); + } + ERR_FAIL_COND_V_MSG(!element, Variant(), "The given Dictionary does not contain value! " + var_name + " Full variable: " + variable); String var_index = variable.substr_index(lsqbrace_pos + 1, rsqbrace_pos).lstrip("(").rstrip(")").strip_edges(); @@ -578,7 +590,7 @@ method_name_search_done: for (uint32_t vi = 0; vi < variables.size(); ++vi) { String variable = variables[vi]; - final_values.set(vi, process_template_expression_variable(variable, p_data)); + final_values.set(vi, process_template_expression_variable(variable, p_data, first_var_decides_print && vi == 0)); } return call_template_method(call_method, final_values, first_var_decides_print); @@ -1091,7 +1103,7 @@ void HTMLTemplate::_bind_methods() { ClassDB::bind_method(D_METHOD("get_template_text", "name"), &HTMLTemplate::get_template_text); ClassDB::bind_method(D_METHOD("call_template_method", "method", "data", "first_var_decides_print"), &HTMLTemplate::call_template_method); - ClassDB::bind_method(D_METHOD("process_template_expression_variable", "variable", "data"), &HTMLTemplate::process_template_expression_variable); + ClassDB::bind_method(D_METHOD("process_template_expression_variable", "variable", "data", "allow_missing"), &HTMLTemplate::process_template_expression_variable, false); ClassDB::bind_method(D_METHOD("process_template_expression", "expression", "data"), &HTMLTemplate::process_template_expression); ClassDB::bind_method(D_METHOD("render_template", "text", "data"), &HTMLTemplate::render_template); diff --git a/modules/web/html/html_template.h b/modules/web/html/html_template.h index 4af769219..d46a5bd40 100644 --- a/modules/web/html/html_template.h +++ b/modules/web/html/html_template.h @@ -98,7 +98,7 @@ public: String get_template_text(const StringName &p_name); String call_template_method(const TemplateExpressionMethods p_method, const Array &p_data, const bool p_first_var_decides_print); - Variant process_template_expression_variable(const String &p_variable, const Dictionary &p_data); + Variant process_template_expression_variable(const String &p_variable, const Dictionary &p_data, const bool p_allow_missing = false); String process_template_expression(const String &p_expression, const Dictionary &p_data); String render_template(const String &p_text, const Dictionary &p_data);