renderTemplate function

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

Implementation

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

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

  String content = await file.readAsString();

  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;
}