Skip to content

Commit

Permalink
Plugins/JTemplate: Added published values to fields. (patch from @Al-…
Browse files Browse the repository at this point in the history
  • Loading branch information
silvioprog committed Jan 24, 2019
1 parent 1150403 commit c9fd6b1
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion plugins/jtemplate/src/jtemplate.pas
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
interface

uses
SysUtils, StrUtils, Classes, FPJSON;
SysUtils, StrUtils, Classes, FPJSON, typinfo;

type
EJTemplate = class(Exception);
Expand Down Expand Up @@ -93,6 +93,7 @@ TJTemplate = class(TComponent)
function GetTagEscape: string;
function GetTagPrefix: string;
function GetTagSuffix: string;
procedure ParserAddFields(const ASection, Item, Value: string);
procedure SetContent(AValue: TStrings);
procedure SetFields(AValue: TJSONObject);
procedure SetHtmlSupports(AValue: Boolean);
Expand All @@ -107,6 +108,9 @@ TJTemplate = class(TComponent)
procedure FreeStream; virtual;
function GetStreamClass: TJTemplateStreamClass;
public
procedure AddFields(AnObject: TObject; ASection: String = '';
PropKinds: TTypeKinds = [tkInteger,tkSString,tkLString,tkAString,tkWString,tkBool,tkInt64,
tkFloat]);
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Replace(const ARecursive: Boolean = False);
Expand Down Expand Up @@ -158,6 +162,9 @@ function StrToHtml(const S: string): string;

implementation

uses
rttiutils;

function StrToHtml(const S: string): string;

function _Found(const ABuf: PChar; const ALen: Integer): Integer; inline;
Expand Down Expand Up @@ -360,6 +367,29 @@ function TJTemplate.GetStreamClass: TJTemplateStreamClass;
Result := TJTemplateStream;
end;

procedure TJTemplate.AddFields(AnObject: TObject; ASection: String;
PropKinds: TTypeKinds);
var
i: Integer;
APropsStorage: TPropsStorage;
APropList: TPropInfoList;
begin
if not Assigned(AnObject) then
Exit;
APropsStorage:=TPropsStorage.Create;
APropsStorage.AObject:=AnObject;
APropsStorage.OnWriteString:=@ParserAddFields;
APropsStorage.Section:=ASection;
APropList:=TPropInfoList.Create(AnObject, PropKinds);
try
for i:=APropList.Count-1 downto 0 do
APropsStorage.StoreAnyProperty(APropList[i]);
finally
APropList.Free;
APropsStorage.Free;
end;
end;

function TJTemplate.GetContent: TStrings;
begin
Result := FContent;
Expand Down Expand Up @@ -402,6 +432,17 @@ function TJTemplate.GetTagSuffix: string;
Result := FStream.FParser.FTagSuffix;
end;

procedure TJTemplate.ParserAddFields(const ASection, Item, Value: string);
var
AName: String;
begin
if ASection<>EmptyStr then
AName:=ASection+'.'+Item
else
AName:=Item;
Parser.Fields.Add(AnsiLowerCase(AName), Value);
end;

procedure TJTemplate.SetContent(AValue: TStrings);
begin
if Assigned(AValue) then
Expand Down

0 comments on commit c9fd6b1

Please sign in to comment.