Sign in Join
FPDoc Onlineexperiment

Parametrized Template Usage topic

In unit fpTemplate

Using templates with parameters

Declaration

pascal
Parametrized Template Usage

Description

TTemplateParser supports parametrized templates where tags can have parameters. To use parametrized templates, set AllowTagParams to True and use the OnReplaceTag event instead of OnGetParam. Parameters are enclosed in square brackets with the format {TagName[-param1=value1-][-param2=value2-]}.

The following example demonstrates parametrized template usage:

program ParametrizedTemplateExample;

{$mode objfpc}{$h+}

uses
  fpTemplate, SysUtils, Classes;

type
  TEventHandler = class
    procedure ReplaceTagHandler(Sender: TObject; const TagString: string;
      TagParams: TStringList; out ReplaceText: string);
  end;

procedure TEventHandler.ReplaceTagHandler(Sender: TObject; const TagString: string;
  TagParams: TStringList; out ReplaceText: string);
var
  i: Integer;
  ParamName, ParamValue: string;
begin
  if TagString = 'FORMAT' then
  begin
    // Handle FORMAT tag with parameters
    ReplaceText := 'Formatted text';
    for i := 0 to TagParams.Count - 1 do
    begin
      ParamName := TagParams.Names[i];
      ParamValue := TagParams.ValueFromIndex[i];
      if ParamName = 'style' then
      begin
        if ParamValue = 'bold' then
          ReplaceText := '**' + ReplaceText + '**'
        else if ParamValue = 'italic' then
          ReplaceText := '*' + ReplaceText + '*';
      end
      else if ParamName = 'color' then
        ReplaceText := '[' + ParamValue + ']' + ReplaceText + '[/' + ParamValue + ']';
    end;
  end
  else if TagString = 'REPEAT' then
  begin
    // Handle REPEAT tag with count parameter
    ReplaceText := '';
    for i := 0 to TagParams.Count - 1 do
    begin
      ParamName := TagParams.Names[i];
      ParamValue := TagParams.ValueFromIndex[i];
      if ParamName = 'count' then
      begin
        ReplaceText := StringOfChar('*', StrToIntDef(ParamValue, 1));
        Break;
      end;
    end;
  end
  else if TagString = 'TABLE' then
  begin
    // Handle TABLE tag with rows and cols parameters
    ReplaceText := 'Table';
    for i := 0 to TagParams.Count - 1 do
    begin
      ParamName := TagParams.Names[i];
      ParamValue := TagParams.ValueFromIndex[i];
      if ParamName = 'rows' then
        ReplaceText := ReplaceText + '(' + ParamValue + ' rows'
      else if ParamName = 'cols' then
        ReplaceText := ReplaceText + ', ' + ParamValue + ' cols)';
    end;
  end
  else
    ReplaceText := 'Unknown tag: ' + TagString;
end;

var
  Parser: TTemplateParser;
  Handler: TEventHandler;
  Template, Result: string;
begin
  Parser := TTemplateParser.Create;
  Handler := TEventHandler.Create;
  try
    // Enable parametrized templates
    Parser.AllowTagParams := True;

    // Assign tag replacement handler
    Parser.OnReplaceTag := @Handler.ReplaceTagHandler;

    // Define template text with parametrized tags
    Template := 'This is {FORMAT[-style=bold-]} text with {FORMAT[-style=italic-][-color=red-]} formatting.' + LineEnding +
                'Here are some {REPEAT[-count=5-]} stars.' + LineEnding +
                'And here is a {TABLE[-rows=3-][-cols=4-]} definition.';

    // Parse the template
    Result := Parser.ParseString(Template);

    WriteLn(Result);
  finally
    Handler.Free;
    Parser.Free;
  end;
end.

This will output:

This is **Formatted text** text with [red]*Formatted text*[/red] formatting.
Here are some ***** stars.
And here is a Table(3 rows, 4 cols) definition.

You can also use parametrized templates with TFPTemplate:

program ParametrizedTFPTemplateExample;

{$mode objfpc}{$h+}

uses
  fpTemplate, SysUtils, Classes;

type
  TEventHandler = class
    procedure ReplaceTagHandler(Sender: TObject; const TagString: string;
      TagParams: TStringList; out ReplaceText: string);
  end;

procedure TEventHandler.ReplaceTagHandler(Sender: TObject; const TagString: string;
  TagParams: TStringList; out ReplaceText: string);
var
  i: Integer;
  ParamName, ParamValue: string;
begin
  if TagString = 'LINK' then
  begin
    // Handle LINK tag with url and text parameters
    ReplaceText := '#';
    for i := 0 to TagParams.Count - 1 do
    begin
      ParamName := TagParams.Names[i];
      ParamValue := TagParams.ValueFromIndex[i];
      if ParamName = 'url' then
        ReplaceText := '<a href="' + ParamValue + '"|>'
      else if ParamName = 'text' then
        ReplaceText := ReplaceText + ParamValue + '</a>';
    end;
  end
  else if TagString = 'IMG' then
  begin
    // Handle IMG tag with src and alt parameters
    ReplaceText := '<img';
    for i := 0 to TagParams.Count - 1 do
    begin
      ParamName := TagParams.Names[i];
      ParamValue := TagParams.ValueFromIndex[i];
      ReplaceText := ReplaceText + ' ' + ParamName + '="' + ParamValue + '"';
    end;
    ReplaceText := ReplaceText + ' />';
  end
  else
    ReplaceText := '';
end;

var
  Template: TFPTemplate;
  Handler: TEventHandler;
  Result: string;
begin
  Template := TFPTemplate.Create;
  Handler := TEventHandler.Create;
  try
    // Enable parametrized templates
    Template.AllowTagParams := True;

    // Set template content
    Template.Template := 'Visit our {LINK[-url=http://www.freepascal.org-][-text=website-]} for more information.' + LineEnding +
                         'Logo: {IMG[-src=logo.png-][-alt=FreePascal Logo-]}';

    // Assign tag replacement handler
    Template.OnReplaceTag := @Handler.ReplaceTagHandler;

    // Get parsed content
    Result := Template.GetContent;

    WriteLn(Result);
  finally
    Handler.Free;
    Template.Free;
  end;
end.

This will output:

<p>Visit our <a href="http://www.freepascal.org">website</a> for more information.</p>
<p>Logo: <img src="logo.png" alt="FreePascal Logo" /></p>

Note that when using parametrized templates:

  • Set AllowTagParams to True
  • Use OnReplaceTag event instead of OnGetParam
  • Parameters use the format [-paramname=value-]
  • The Values property is not used in parametrized mode

See also

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.