On Fri, 27 Jul 2012, luciano de souza wrote:
Hello all,
I am trying to create an example with the FPTemplate unit. Firstly, I
wrote a hypothetical unit template.
[snip]
I am not successful in filling up the template. If I do
writeln(source.template), the answer is an empty string. If I do
writeln(source.GetContent), the answer is the template without the
needed replacements.
So I ask: what is wrong? How can I get a file, to replace tags and to
get a replaced string?
1. You need only one of the two classes, never both at the same time.
2. You need to set the correct delimiters. Default are { and }
If you want to have fixed values for parameters, the easiest is
TTemplateParser. TFPCustomTemplate (or TFPTemplate) does not have
fixed values, everything is event driven.
So, the following will do what you want:
program e30;
{$mode objfpc}{$H+}
uses
fptemplate, classes;
var
T : TTemplateParser;
Fin,Fout : TFileStream;
BEGIN
T := TTemplateParser.create;
try
T.StartDelimiter:='<#';
T.EndDelimiter:='>';
T.values['name'] := 'freevox';
T.values['modules'] := 'sysutils, classes';
T.AllowTagParams:=False;
Fin:=TFileStream.Create('e30.txt',fmOpenRead);
try
Fout:=TFileStream.Create('freevox.pas',fmCreate);
try
T.ParseStream(fin,fout);
finally
Fout.Free;
end;
finally
Fin.Free;
end;
finally
T.free;
end;
end.
Running this produces the file you want:
fsb: >e30
fsb: >cat freevox.pas
unit freevox;
{$mode objfpc}{$H+}
interface
uses
sysutils, classes
implementation
end.
That's it.
In revision 21977, I added a ParseFiles call, which makes the program even
easier:
program e30;
{$mode objfpc}{$H+}
uses
fptemplate, classes;
var
T : TTemplateParser;
BEGIN
T := TTemplateParser.create;
try
T.StartDelimiter:='<#';
T.EndDelimiter:='>';
T.values['name'] := 'freevox';
T.values['modules'] := 'sysutils, classes';
T.AllowTagParams:=False;
T.ParseFiles('e30.txt','freevox.pas');
finally
T.free;
end;
end.
Michael.
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal