resolveVariable method
dynamic
resolveVariable(
- dynamic input
Resolves the given input with any potential variable on the controller.
Variable values use the mustache format and must begin with {{
and end
with }}
. If the input is not marked as a variable or there is no
variable with the matching key registered then the input will be return
unaltered.
Implementation
dynamic resolveVariable(dynamic input) {
dynamic result = input;
if (input is String && input.contains('{{') && input.contains('}}')) {
var regex = RegExp(r'\{\{[^(})]*}}]*');
var matches = regex.allMatches(input);
if (matches.isNotEmpty == true) {
if (matches.length == 1 &&
input.startsWith('{{') &&
input.endsWith('}}')) {
var match = matches.first;
var variableName =
result.substring(match.start + 2, match.end - 2).trim();
var value = getVariable(variableName);
if (value != null ||
_testVariables.containsKey(variableName) == true ||
_globalVariables.containsKey(variableName) == true) {
result = value;
}
} else {
matches = matches.toList().reversed;
for (var match in matches) {
var variableName =
result.substring(match.start + 2, match.end - 2).trim();
var value = getVariable(variableName);
if (value != null ||
_testVariables.containsKey(variableName) == true ||
_globalVariables.containsKey(variableName) == true) {
result = value == null
? null
: '${result.substring(0, match.start)}$value${result.substring(match.end, result.length)}';
}
}
}
}
}
return result;
}