renderTemplate function

String renderTemplate(
  1. String templatePath,
  2. Map<String, String> vars
)

Implementation

String renderTemplate(String templatePath, Map<String, String> vars) {
  final fullPath = PathResolver.getTemplatePath(templatePath);
  final file = File(fullPath);

  if (!file.existsSync()) {
    throw Exception('Template not found: $templatePath');
  }

  String content = file.readAsStringSync();

  for (final entry in vars.entries) {
    final key = entry.key;
    final value = _escapeForReplacement(entry.value);
    final pattern = RegExp(r'\{\{\s*' + RegExp.escape(key) + r'\s*\}\}');
    content = content.replaceAllMapped(pattern, (match) => value);
  }

  final unreplaced = RegExp(r'\{\{.*?\}\}').allMatches(content);
  if (unreplaced.isNotEmpty) {
    final missingVars = unreplaced.map((m) => m.group(0)).toList();
    print('⚠️ Warning: Unreplaced template variables: $missingVars');
  }

  return content;
}