10 changed files with 208 additions and 47 deletions
Binary file not shown.
@ -0,0 +1,57 @@ |
|||||||
|
// mustache_service.dart |
||||||
|
import 'package:mustache_template/mustache_template.dart'; |
||||||
|
|
||||||
|
class MustacheService { |
||||||
|
// Validate Mustache template syntax |
||||||
|
static bool validateTemplate(String template) { |
||||||
|
try { |
||||||
|
Template(template); |
||||||
|
return true; |
||||||
|
} catch (e) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Get variables from template |
||||||
|
static Set<String> getTemplateVariables(String template) { |
||||||
|
try { |
||||||
|
final parsed = Template(template); |
||||||
|
// Use the source to parse variables manually |
||||||
|
return _parseVariablesFromSource(parsed.source); |
||||||
|
} catch (e) { |
||||||
|
return {}; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Apply template to a row of data |
||||||
|
static String applyTemplate(String template, Map<String, dynamic> data) { |
||||||
|
try { |
||||||
|
return Template(template).renderString(data); |
||||||
|
} catch (e) { |
||||||
|
return 'Error applying template: $e'; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Helper method to parse variables from template source |
||||||
|
static Set<String> _parseVariablesFromSource(String source) { |
||||||
|
final variables = <String>{}; |
||||||
|
final pattern = RegExp(r'{{\s*([^{}\s]+)\s*}}'); |
||||||
|
final matches = pattern.allMatches(source); |
||||||
|
|
||||||
|
for (final match in matches) { |
||||||
|
if (match.groupCount >= 1) { |
||||||
|
final variable = match.group(1)!; |
||||||
|
// Skip sections and special tags |
||||||
|
if (!variable.startsWith('#') && |
||||||
|
!variable.startsWith('/') && |
||||||
|
!variable.startsWith('^') && |
||||||
|
!variable.startsWith('>') && |
||||||
|
!variable.startsWith('!')) { |
||||||
|
variables.add(variable); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return variables; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue