Mator

ApplyTemplate test

Jul 1st, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.88 KB | None | 0 0
  1. {
  2.   Tests the ApplyTemplate function.
  3. }
  4. unit userscript;
  5.  
  6. function ApplyTemplate(const template: string; var map: TStringList): string;
  7. const
  8.   openTag = '{{';
  9.   closeTag = '}}';
  10. var
  11.   i: Integer;
  12.   name, value: string;
  13. begin
  14.   Result := template;
  15.   for i := 0 to Pred(map.Count) do begin
  16.     name := map.Names[i];
  17.     value := map.ValueFromIndex[i];
  18.     Result := StringReplace(Result, openTag + name + closeTag, value, [rfReplaceAll]);
  19.   end;
  20. end;
  21.  
  22. function Initialize: integer;
  23. var
  24.   sl: TStringList;
  25.   template, userString: string;
  26. begin
  27.   sl := TStringList.Create;
  28.   sl.Values['user'] := 'Mator';
  29.   sl.Values['date'] := '07/01/2015';
  30.   sl.Values['title'] := 'autoMator';
  31.   template := '<{{user}} : {{title}}> Posted {{date}}';
  32.   userString := ApplyTemplate(template, sl);
  33.   AddMessage(userString); // <Mator : autoMator> Posted 07/01/2015
  34.   sl.Free;
  35. end;
  36.  
  37. end.
Advertisement
Add Comment
Please, Sign In to add comment