template function

String template(
  1. String str,
  2. Map<String, String> data
)

Replaces the templating placeholders with the provided data map.

Throws an Exception if any placeholder remains unresolved.

Implementation

String template(String str, Map<String, String> data) {
  return str.replaceAllMapped(_templateRe, (Match match) {
    var value = data[match.group(1)];
    if (value == null) {
      throw Exception('No value provided for variable ${match.group(1)}');
    } else {
      return value;
    }
  });
}