DataModelUtils.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using InABox.Core;
  4. using Scriban;
  5. using Scriban.Runtime;
  6. namespace PRSDesktop
  7. {
  8. public static class DataModelUtils
  9. {
  10. public static string ParseTemplate(DataModel model, string data)
  11. {
  12. var template = Template.Parse(data);
  13. if (template.HasErrors) throw new Exception(string.Join("\n", template.Messages));
  14. var templatecontext = new TemplateContext
  15. {
  16. MemberRenamer = member => member.Name
  17. };
  18. var so = new ScriptObject();
  19. ScriptObjectExtensions.Import(so, "string.to_base64", new Func<byte[], string>(b => Convert.ToBase64String(b)));
  20. var tables = model.AsDictionary;
  21. foreach (var key in tables.Keys)
  22. {
  23. var objects = new List<object>();
  24. foreach (var tablerow in tables[key].Rows)
  25. objects.Add(tablerow.ToObject(key));
  26. so.Add(key.Name, objects);
  27. }
  28. templatecontext.PushGlobal(so);
  29. return template.Render(templatecontext);
  30. }
  31. }
  32. }