Basic Template Usage topic
Basic usage of the template parser
Declaration
Basic Template UsageDescription
TTemplateParser allows you to replace variables in template text with actual values.
Variables are enclosed in delimiters (by default
{ and
}).
The following example demonstrates basic template parsing with simple variables:
program BasicTemplateExample;
{$mode objfpc}{$h+}
uses
fpTemplate, SysUtils;
var
Parser: TTemplateParser;
Template, Result: string;
begin
Parser := TTemplateParser.Create;
try
// Set up template values
Parser.Values['Name'] := 'John Doe';
Parser.Values['Age'] := '30';
Parser.Values['City'] := 'Amsterdam';
// Define template text
Template := 'Hello {Name}! You are {Age} years old and live in {City}.';
// Parse the template
Result := Parser.ParseString(Template);
WriteLn(Result);
finally
Parser.Free;
end;
end.
This will output:
Hello John Doe! You are 30 years old and live in Amsterdam.
You can also use an event handler to provide values dynamically:
program EventTemplateExample;
{$mode objfpc}{$h+}
uses
fpTemplate, SysUtils;
type
TEventHandler = class
procedure GetParamValue(Sender: TObject; const ParamName: string; out AValue: string);
end;
procedure TEventHandler.GetParamValue(Sender: TObject; const ParamName: string; out AValue: string);
begin
case ParamName of
'CurrentDate': AValue := DateToStr(Now);
'CurrentTime': AValue := TimeToStr(Now);
'UserName': AValue := GetEnvironmentVariable('USER');
else
AValue := 'Unknown';
end;
end;
var
Parser: TTemplateParser;
Handler: TEventHandler;
Template, Result: string;
begin
Parser := TTemplateParser.Create;
Handler := TEventHandler.Create;
try
// Assign event handler
Parser.OnGetParam := @Handler.GetParamValue;
// Define template text
Template := 'Date: {CurrentDate}, Time: {CurrentTime}, User: {UserName}';
// Parse the template
Result := Parser.ParseString(Template);
WriteLn(Result);
finally
Handler.Free;
Parser.Free;
end;
end.
The TFPTemplate class provides a higher-level interface that can load templates from files:
program FileTemplateExample;
{$mode objfpc}{$h+}
uses
fpTemplate, SysUtils;
type
TEventHandler = class
procedure GetParamValue(Sender: TObject; const ParamName: string; out AValue: string);
end;
procedure TEventHandler.GetParamValue(Sender: TObject; const ParamName: string; out AValue: string);
begin
case ParamName of
'Title': AValue := 'Welcome Page';
'Content': AValue := 'This is the main content of the page.';
'Footer': AValue := 'Copyright 2024 Example Company';
else
AValue := '';
end;
end;
var
Template: TFPTemplate;
Handler: TEventHandler;
Result: string;
begin
Template := TFPTemplate.Create;
Handler := TEventHandler.Create;
try
// Set template content directly
Template.Template := '
{Title}
' +
'
{Title}
{Content}' +
'
{Footer}
';
// Assign event handler
Template.OnGetParam := @Handler.GetParamValue;
// Get parsed content
Result := Template.GetContent;
WriteLn(Result);
finally
Handler.Free;
Template.Free;
end;
end.See also
An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
