PlaceholderParser constructor
PlaceholderParser()
A utility class for parsing and resolving placeholders within a string.
Supports placeholder expressions of the form:
Hello, #{user.name:Guest}! —> // Output: Hello, Alice! if user.name resolves to "Alice"
or "Hello, Guest!" if it does not.
Supports nested placeholders and fallback values:
#{app.title:#{default.title:MyApp}}
Example usage:
final parser = PlaceholderParser(r'#{', '}', ':', r'\', false);
final result = parser.replacePlaceholders('Welcome #{user.name:Guest}', MyResolver());
print(result); // Welcome Alice or Welcome Guest
You can control:
- Prefix/suffix (e.g.,
#{...}
or[[...]]
) - Escape characters (e.g.,
\#{...}
to skip parsing) - Fallback separator (e.g.,
:
or|
) - Whether to ignore unresolvable placeholders
Implementation
PlaceholderParser(
this._prefix,
this._suffix,
this._separator,
this._escape,
this._ignoreUnresolvablePlaceholders,
) {
String? simplePrefixForSuffix = wellKnownSimplePrefixes[_suffix];
if (simplePrefixForSuffix != null && _prefix.endsWith(simplePrefixForSuffix)) {
_simplePrefix = simplePrefixForSuffix;
} else {
_simplePrefix = _prefix;
}
}