demo2.lpr 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. { Demo for mustache engine with database context
  2. Copyright (C) 2021 michael Van Canneyt [email protected]
  3. This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
  4. published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  5. This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  6. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. A copy of the GNU General Public License is available on the World Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can
  8. also obtain it by writing to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
  9. }
  10. program demo2;
  11. uses csvdataset, fpmustache, fpdbmustache;
  12. Const
  13. // Mock markdown table
  14. Template =
  15. '| name | age | '+sLineBreak+
  16. '|------|------|'+sLineBreak+
  17. '{{#family}}| {{name}} | {{age}} |'+sLineBreak+
  18. '{{/family}}';
  19. Var
  20. M : TMustache;
  21. C : TMustacheDBContext;
  22. D : TCSVDataset;
  23. begin
  24. M:=TMustache.Create(Nil);
  25. try
  26. D:=TCSVDataset.Create(Nil);
  27. D.CSVOptions.FirstLineAsFieldNames:=True;
  28. D.LoadFromFile('family.csv');
  29. C:=TMustacheDBContext.Create(Nil);
  30. C.AddDataset(D,'family');
  31. M.Template:=Template;
  32. Writeln(M.Render(C));
  33. finally
  34. M.Free;
  35. D.Free;
  36. C.Free;
  37. end;
  38. end.