mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2025-01-11 13:21:10 +01:00
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.
This commit is contained in:
parent
aa854f049a
commit
df4379d766
@ -158,6 +158,7 @@
|
|||||||
<return type="Variant" />
|
<return type="Variant" />
|
||||||
<argument index="0" name="variable" type="String" />
|
<argument index="0" name="variable" type="String" />
|
||||||
<argument index="1" name="data" type="Dictionary" />
|
<argument index="1" name="data" type="Dictionary" />
|
||||||
|
<argument index="2" name="allow_missing" type="bool" default="false" />
|
||||||
<description>
|
<description>
|
||||||
Processes one variable from an expression in a template. An expression in the template look like: [code]{{ EXPRESSION }}[/code].
|
Processes one variable from an expression in a template. An expression in the template look like: [code]{{ EXPRESSION }}[/code].
|
||||||
</description>
|
</description>
|
||||||
|
@ -301,7 +301,7 @@ String HTMLTemplate::call_template_method(const TemplateExpressionMethods p_meth
|
|||||||
return String();
|
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
|
||||||
// 'XXX' // String
|
// 'XXX' // String
|
||||||
// var[1] // Array indexing
|
// 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));
|
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);
|
ERR_FAIL_COND_V_MSG(!element, Variant(), "The given Dictionary does not contain value! " + variable);
|
||||||
|
|
||||||
return *element;
|
return *element;
|
||||||
@ -341,10 +345,14 @@ Variant HTMLTemplate::process_template_expression_variable(const String &p_varia
|
|||||||
|
|
||||||
int rsqbrace_pos = variable.find_last("]");
|
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) {
|
if (rsqbrace_pos == -1) {
|
||||||
const Variant *element = p_data.getptr(Variant(p_variable));
|
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);
|
ERR_FAIL_COND_V_MSG(!element, Variant(), "The given Dictionary does not contain value! " + variable);
|
||||||
|
|
||||||
return *element;
|
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));
|
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);
|
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();
|
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) {
|
for (uint32_t vi = 0; vi < variables.size(); ++vi) {
|
||||||
String variable = variables[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);
|
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("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("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("process_template_expression", "expression", "data"), &HTMLTemplate::process_template_expression);
|
||||||
ClassDB::bind_method(D_METHOD("render_template", "text", "data"), &HTMLTemplate::render_template);
|
ClassDB::bind_method(D_METHOD("render_template", "text", "data"), &HTMLTemplate::render_template);
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ public:
|
|||||||
String get_template_text(const StringName &p_name);
|
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);
|
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 process_template_expression(const String &p_expression, const Dictionary &p_data);
|
||||||
String render_template(const String &p_text, const Dictionary &p_data);
|
String render_template(const String &p_text, const Dictionary &p_data);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user